Skip to content

Commit 675381a

Browse files
authored
fix: Connector "Type" cannot be Serverless resource types (#3088)
1 parent b3dd76c commit 675381a

File tree

5 files changed

+1169
-2
lines changed

5 files changed

+1169
-2
lines changed

samtranslator/model/connector/connector.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
from typing import Any, Dict, Iterable, List, Optional
33

44
from samtranslator.model import ResourceResolver
5+
from samtranslator.model.apigateway import ApiGatewayRestApi
6+
from samtranslator.model.apigatewayv2 import ApiGatewayV2HttpApi
7+
from samtranslator.model.dynamodb import DynamoDBTable
58
from samtranslator.model.intrinsics import fnGetAtt, get_logical_id_from_intrinsic, ref
9+
from samtranslator.model.lambda_ import (
10+
LambdaFunction,
11+
)
12+
from samtranslator.model.stepfunctions import StepFunctionsStateMachine
13+
from samtranslator.public.sdk.resource import SamResourceType
614
from samtranslator.utils.utils import as_array, insert_unique
715

816
# TODO: Switch to dataclass
@@ -20,6 +28,14 @@
2028
],
2129
)
2230

31+
_SAM_TO_CFN_RESOURCE_TYPE = {
32+
SamResourceType.Function.value: LambdaFunction.resource_type,
33+
SamResourceType.StateMachine.value: StepFunctionsStateMachine.resource_type,
34+
SamResourceType.Api.value: ApiGatewayRestApi.resource_type,
35+
SamResourceType.HttpApi.value: ApiGatewayV2HttpApi.resource_type,
36+
SamResourceType.SimpleTable.value: DynamoDBTable.resource_type,
37+
}
38+
2339
UNSUPPORTED_CONNECTOR_PROFILE_TYPE = "UNSUPPORTED_CONNECTOR_PROFILE_TYPE"
2440

2541

@@ -98,14 +114,16 @@ def get_resource_reference(
98114
)
99115

100116
logical_id = obj.get("Id")
101-
102117
# Must provide Id (with optional Qualifier) or a supported combination of other properties
103118
# If Id is not provided, all values must come from overrides.
104119
if not logical_id:
105120
resource_type = obj.get("Type")
106121
if not _is_nonblank_str(resource_type):
107122
raise ConnectorResourceError("'Type' is missing or not a string.")
108-
resource_type = str(resource_type)
123+
124+
# profiles.json only support CFN resource type.
125+
# We need to convert SAM resource types to corresponding CFN resource type
126+
resource_type = _SAM_TO_CFN_RESOURCE_TYPE.get(str(resource_type), str(resource_type))
109127

110128
return ConnectorResourceReference(
111129
logical_id=None,
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
Transform: AWS::Serverless-2016-10-31
2+
3+
Resources:
4+
MyRole:
5+
Type: AWS::IAM::Role
6+
Properties:
7+
AssumeRolePolicyDocument:
8+
Statement:
9+
- Effect: Allow
10+
Action: sts:AssumeRole
11+
Principal:
12+
Service: lambda.amazonaws.com
13+
ManagedPolicyArns:
14+
- arn:{AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
15+
16+
SamFunction:
17+
Type: AWS::Serverless::Function
18+
Properties:
19+
Runtime: nodejs14.x
20+
Handler: index.handler
21+
Role: !GetAtt MyRole.Arn
22+
InlineCode: |
23+
const AWS = require('aws-sdk');
24+
exports.handler = async (event) => {
25+
console.log(JSON.stringify(event));
26+
};
27+
28+
SamTable:
29+
Type: AWS::Serverless::SimpleTable
30+
Properties:
31+
PrimaryKey:
32+
Name: NoteId
33+
Type: String
34+
35+
SamStateMachine:
36+
Type: AWS::Serverless::StateMachine
37+
Properties:
38+
# Express state machine support sync execution
39+
# which allows us to get the error message quickly in trigger function.
40+
Type: EXPRESS
41+
Role: !GetAtt MyRole.Arn
42+
Definition:
43+
StartAt: MyLambdaState
44+
States:
45+
MyLambdaState:
46+
Type: Task
47+
Resource: !GetAtt SamFunction.Arn
48+
End: true
49+
50+
SamApi:
51+
Type: AWS::Serverless::Api
52+
Properties:
53+
StageName: Prod
54+
DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json
55+
Description: my description
56+
57+
SamHttpApi:
58+
Type: AWS::Serverless::HttpApi
59+
Properties:
60+
StageName: Prod
61+
62+
Connector1:
63+
Type: AWS::Serverless::Connector
64+
Properties:
65+
Source:
66+
Type: AWS::Serverless::Function
67+
RoleName: MyRole
68+
Destination:
69+
Type: AWS::Serverless::SimpleTable
70+
Arn: !GetAtt SamTable.Arn
71+
Permissions:
72+
- Read
73+
74+
Connector2:
75+
Type: AWS::Serverless::Connector
76+
Properties:
77+
Source:
78+
Type: AWS::Serverless::Api
79+
ResourceId: !Ref SamApi
80+
Qualifier: Prod/GET/foobar
81+
Destination:
82+
Type: AWS::Serverless::Function
83+
Arn: !GetAtt SamFunction.Arn
84+
Permissions:
85+
- Write
86+
87+
Connector3:
88+
Type: AWS::Serverless::Connector
89+
Properties:
90+
Source:
91+
Type: AWS::Serverless::StateMachine
92+
RoleName: MyRole
93+
Destination:
94+
Type: AWS::Serverless::Function
95+
Arn: !GetAtt SamFunction.Arn
96+
Permissions:
97+
- Write
98+
99+
Connector4:
100+
Type: AWS::Serverless::Connector
101+
Properties:
102+
Source:
103+
Type: AWS::Serverless::HttpApi
104+
ResourceId: !Ref SamHttpApi
105+
Qualifier: Prod/GET/foobar
106+
Destination:
107+
Type: AWS::Serverless::Function
108+
Arn: !GetAtt SamFunction.Arn
109+
Permissions:
110+
- Write

0 commit comments

Comments
 (0)