Skip to content

Commit

Permalink
fix: Connector "Type" cannot be Serverless resource types (#3088)
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinZZ committed Apr 5, 2023
1 parent b3dd76c commit 675381a
Show file tree
Hide file tree
Showing 5 changed files with 1,169 additions and 2 deletions.
22 changes: 20 additions & 2 deletions samtranslator/model/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
from typing import Any, Dict, Iterable, List, Optional

from samtranslator.model import ResourceResolver
from samtranslator.model.apigateway import ApiGatewayRestApi
from samtranslator.model.apigatewayv2 import ApiGatewayV2HttpApi
from samtranslator.model.dynamodb import DynamoDBTable
from samtranslator.model.intrinsics import fnGetAtt, get_logical_id_from_intrinsic, ref
from samtranslator.model.lambda_ import (
LambdaFunction,
)
from samtranslator.model.stepfunctions import StepFunctionsStateMachine
from samtranslator.public.sdk.resource import SamResourceType
from samtranslator.utils.utils import as_array, insert_unique

# TODO: Switch to dataclass
Expand All @@ -20,6 +28,14 @@
],
)

_SAM_TO_CFN_RESOURCE_TYPE = {
SamResourceType.Function.value: LambdaFunction.resource_type,
SamResourceType.StateMachine.value: StepFunctionsStateMachine.resource_type,
SamResourceType.Api.value: ApiGatewayRestApi.resource_type,
SamResourceType.HttpApi.value: ApiGatewayV2HttpApi.resource_type,
SamResourceType.SimpleTable.value: DynamoDBTable.resource_type,
}

UNSUPPORTED_CONNECTOR_PROFILE_TYPE = "UNSUPPORTED_CONNECTOR_PROFILE_TYPE"


Expand Down Expand Up @@ -98,14 +114,16 @@ 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)

# profiles.json only support CFN resource type.
# We need to convert SAM resource types to corresponding CFN resource type
resource_type = _SAM_TO_CFN_RESOURCE_TYPE.get(str(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

0 comments on commit 675381a

Please sign in to comment.