-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat: add UnescapeMappingTemplate
to state machine Api
event
#2495
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ "LogicalResourceId": "HelloWorldFunction", "ResourceType": "AWS::Lambda::Function" }, | ||
{ "LogicalResourceId": "HelloWorldFunctionRole", "ResourceType": "AWS::IAM::Role" }, | ||
{ "LogicalResourceId": "MyApi", "ResourceType": "AWS::ApiGateway::RestApi" }, | ||
{ "LogicalResourceId": "MyApiDeployment", "ResourceType": "AWS::ApiGateway::Deployment" }, | ||
{ "LogicalResourceId": "MyApiProdStage", "ResourceType": "AWS::ApiGateway::Stage" }, | ||
{ "LogicalResourceId": "Post", "ResourceType": "AWS::StepFunctions::StateMachine" }, | ||
{ "LogicalResourceId": "PostPostEchoRole", "ResourceType": "AWS::IAM::Role" }, | ||
{ "LogicalResourceId": "PostRole", "ResourceType": "AWS::IAM::Role" } | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Transform: AWS::Serverless-2016-10-31 | ||
Resources: | ||
MyApi: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
StageName: Prod | ||
HelloWorldFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
InlineCode: | | ||
def handler(event, context): | ||
print(event) | ||
return "do nothing" | ||
Handler: index.handler | ||
Runtime: python3.8 | ||
Post: | ||
Type: AWS::Serverless::StateMachine | ||
Properties: | ||
Policies: | ||
- arn:aws:iam::aws:policy/AWSLambda_FullAccess | ||
Definition: | ||
StartAt: One | ||
States: | ||
One: | ||
Type: Task | ||
Resource: !GetAtt HelloWorldFunction.Arn | ||
End: true | ||
Events: | ||
PostEcho: | ||
Type: Api | ||
Properties: | ||
RestApiId: !Ref MyApi | ||
Path: /echo | ||
Method: POST | ||
UnescapeMappingTemplate: true | ||
|
||
Outputs: | ||
ApiEndpoint: | ||
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/echo" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,6 +252,7 @@ class Api(EventSource): | |
"RestApiId": PropertyType(True, is_str()), | ||
"Stage": PropertyType(False, is_str()), | ||
"Auth": PropertyType(False, is_type(dict)), | ||
"UnescapeMappingTemplate": PropertyType(False, is_type(bool)), | ||
} | ||
|
||
def resources_to_link(self, resources): | ||
|
@@ -356,12 +357,18 @@ def _add_swagger_integration(self, api, resource, role, intrinsics_resolver): | |
if CONDITION in resource.resource_attributes: | ||
condition = resource.resource_attributes[CONDITION] | ||
|
||
request_template = ( | ||
self._generate_request_template_unescaped(resource) | ||
if self.UnescapeMappingTemplate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a very rare use case - If this is a We had a similar discussion for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't "UnescapeMappingTemplate": PropertyType(False, is_type(bool)), already guarantee it's a Edit: for posterity, no. We've deprecated |
||
else self._generate_request_template(resource) | ||
) | ||
|
||
editor.add_state_machine_integration( | ||
self.Path, | ||
self.Method, | ||
integration_uri, | ||
role.get_runtime_attr("arn"), | ||
self._generate_request_template(resource), | ||
request_template, | ||
condition=condition, | ||
) | ||
|
||
|
@@ -453,3 +460,27 @@ def _generate_request_template(self, resource): | |
) | ||
} | ||
return request_templates | ||
|
||
def _generate_request_template_unescaped(self, resource): | ||
"""Generates the Body mapping request template for the Api. This allows for the input | ||
request to the Api to be passed as the execution input to the associated state machine resource. | ||
|
||
Unescapes single quotes such that it's valid JSON. | ||
|
||
:param model.stepfunctions.resources.StepFunctionsStateMachine resource; the state machine | ||
resource to which the Api event source must be associated | ||
|
||
:returns: a body mapping request which passes the Api input to the state machine execution | ||
:rtype: dict | ||
""" | ||
request_templates = { | ||
"application/json": fnSub( | ||
# Need to unescape single quotes escaped by escapeJavaScript. | ||
# Also the mapping template isn't valid JSON, so can't use json.dumps(). | ||
# See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#util-template-reference | ||
"""{"input": "$util.escapeJavaScript($input.json('$')).replaceAll("\\\\'","'")", "stateMachineArn": "${""" | ||
+ resource.logical_id | ||
+ """}"}""" | ||
) | ||
} | ||
return request_templates |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Transform: AWS::Serverless-2016-10-31 | ||
Resources: | ||
MyApi: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
StageName: Prod | ||
HelloWorldFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
InlineCode: | | ||
def handler(event, context): | ||
print(event) | ||
return "do nothing" | ||
Handler: index.handler | ||
Runtime: python3.8 | ||
Post: | ||
Type: AWS::Serverless::StateMachine | ||
Properties: | ||
Policies: | ||
- arn:aws:iam::aws:policy/AWSLambda_FullAccess | ||
Definition: | ||
StartAt: One | ||
States: | ||
One: | ||
Type: Task | ||
Resource: !GetAtt HelloWorldFunction.Arn | ||
End: true | ||
Events: | ||
PostEcho: | ||
Type: Api | ||
Properties: | ||
RestApiId: !Ref MyApi | ||
Path: /echo | ||
Method: POST | ||
UnescapeMappingTemplate: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add something similar to this so we will log the request too?
This will help us investigate in case of flaky test runs