Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Connector "Type" cannot be Serverless resource types #3088

Merged
merged 4 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions samtranslator/model/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ def _is_valid_resource_reference(obj: Dict[str, Any]) -> bool:
return id_provided != non_id_provided


def get_underlying_cfn_resource_type(resource_type: str) -> str:
GavinZZ marked this conversation as resolved.
Show resolved Hide resolved
sam_to_cfn_types = {
"AWS::Serverless::Function": "AWS::Lambda::Function",
"AWS::Serverless::StateMachine": "AWS::StepFunctions::StateMachine",
"AWS::Serverless::Api": "AWS::ApiGateway::RestApi",
"AWS::Serverless::SimpleTable": "AWS::DynamoDB::Table",
"AWS::Serverless::HttpApi": "AWS::ApiGatewayV2::Api",
}
GavinZZ marked this conversation as resolved.
Show resolved Hide resolved
return sam_to_cfn_types.get(resource_type, resource_type)


def get_resource_reference(
obj: Dict[str, Any], resource_resolver: ResourceResolver, connecting_obj: Dict[str, Any]
) -> ConnectorResourceReference:
Expand All @@ -98,14 +109,14 @@ def get_resource_reference(
)

logical_id = obj.get("Id")

# Must provide Id (with optional Qualifier) or a supported combination of other properties
# If Id is not provided, all values must come from overrides.
if not logical_id:
resource_type = obj.get("Type")

if not _is_nonblank_str(resource_type):
raise ConnectorResourceError("'Type' is missing or not a string.")
resource_type = str(resource_type)
resource_type = get_underlying_cfn_resource_type(str(resource_type))

return ConnectorResourceReference(
logical_id=None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
Transform: AWS::Serverless-2016-10-31

Resources:
MyRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
ManagedPolicyArns:
- arn:{AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

SamFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs14.x
Handler: index.handler
Role: !GetAtt MyRole.Arn
InlineCode: |
const AWS = require('aws-sdk');
exports.handler = async (event) => {
console.log(JSON.stringify(event));
};

SamTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: NoteId
Type: String

SamStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
# Express state machine support sync execution
# which allows us to get the error message quickly in trigger function.
Type: EXPRESS
Role: !GetAtt MyRole.Arn
Definition:
StartAt: MyLambdaState
States:
MyLambdaState:
Type: Task
Resource: !GetAtt SamFunction.Arn
End: true

SamApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json
Description: my description

SamHttpApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: Prod

Connector1:
Type: AWS::Serverless::Connector
Properties:
Source:
Type: AWS::Serverless::Function
RoleName: MyRole
Destination:
Type: AWS::Serverless::SimpleTable
Arn: !GetAtt SamTable.Arn
Permissions:
- Read

Connector2:
Type: AWS::Serverless::Connector
Properties:
Source:
Type: AWS::Serverless::Api
ResourceId: !Ref SamApi
Qualifier: Prod/GET/foobar
Destination:
Type: AWS::Serverless::Function
Arn: !GetAtt SamFunction.Arn
Permissions:
- Write

Connector3:
Type: AWS::Serverless::Connector
Properties:
Source:
Type: AWS::Serverless::StateMachine
RoleName: MyRole
Destination:
Type: AWS::Serverless::Function
Arn: !GetAtt SamFunction.Arn
Permissions:
- Write

Connector4:
Type: AWS::Serverless::Connector
Properties:
Source:
Type: AWS::Serverless::HttpApi
ResourceId: !Ref SamHttpApi
Qualifier: Prod/GET/foobar
Destination:
Type: AWS::Serverless::Function
Arn: !GetAtt SamFunction.Arn
Permissions:
- Write
Loading