Skip to content

Commit 6e9847b

Browse files
lxhunterAlex Jäger
and
Alex Jäger
authored
feat: add multiple custom templates (#632)
* feat: add multiple custom templates * fix: indentation * fix: responseParameters were not working as expected --------- Co-authored-by: Alex Jäger <[email protected]>
1 parent 02c6177 commit 6e9847b

File tree

3 files changed

+332
-121
lines changed

3 files changed

+332
-121
lines changed

README.md

+139-102
Original file line numberDiff line numberDiff line change
@@ -9,67 +9,67 @@ Serverless Framework v2.32.0 or later is required.
99

1010
## TOC
1111

12-
- [Install](#install)
13-
- [Setup](#Setup)
14-
- [Adding a custom name for a state machine](#adding-a-custom-name-for-a-statemachine)
15-
- [Adding a custom logical id for a stateMachine](#adding-a-custom-logical-id-for-a-statemachine)
16-
- [Depending on another logical id](#depending-on-another-logical-id)
17-
- [Adding retain property for a state machine](#adding-retain-property-for-a-statemachine)
18-
- [CloudWatch Alarms](#cloudwatch-alarms)
19-
- [CloudWatch Notifications](#cloudwatch-notifications)
20-
- [Blue-Green deployments](#blue-green-deployment)
21-
- [Pre-deployment validation](#pre-deployment-validation)
22-
- [Express Workflow](#express-workflow)
23-
- [CloudWatch Logs](#cloudwatch-logs)
24-
- [X-Ray](#x-ray)
25-
- [Current Gotcha](#current-gotcha)
26-
- [Events](#events)
27-
- [API Gateway](#api-gateway)
28-
- [Simple HTTP endpoint](#simple-http-endpoint)
29-
- [Custom Step Functions Action](#custom-step-functions-action)
30-
- [HTTP Endpoint with custom IAM Role](#http-endpoint-with-custom-iam-role)
31-
- [Share API Gateway and API Resources](#share-api-gateway-and-api-resources)
32-
- [Enabling CORS](#enabling-cors)
33-
- [HTTP Endpoints with AWS_IAM Authorizers](#http-endpoints-with-aws_iam-authorizers)
34-
- [HTTP Endpoints with Custom Authorizers](#http-endpoints-with-custom-authorizers)
35-
- [Shared Authorizer](#shared-authorizer)
36-
- [LAMBDA_PROXY request template](#lambda_proxy-request-template)
37-
- [Customizing request body mapping templates](#customizing-request-body-mapping-templates)
38-
- [Customizing response headers and templates](#customizing-response-headers-and-templates)
39-
- [Send request to an API](#send-request-to-an-api)
40-
- [Setting API keys for your Rest API](#setting-api-keys-for-your-rest-api)
41-
- [Request Schema Validators](#request-schema-validators)
42-
- [Schedule](#schedule)
43-
- [Enabling / Disabling](#enabling--disabling)
44-
- [Specify Name and Description](#specify-name-and-description)
45-
- [Scheduled Events IAM Role](#scheduled-events-iam-role)
46-
- [Specify InputTransformer](#specify-inputtransformer)
47-
- [Use EventBridge Scheduler instead of EventBridge rules](#use-eventbridge-scheduler-instead-of-eventbridge-rules)
48-
- [CloudWatch Event](#cloudwatch-event)
49-
- [Simple event definition](#simple-event-definition)
50-
- [Enabling / Disabling](#enabling--disabling-1)
51-
- [Specify Input or Inputpath or InputTransformer](#specify-input-or-inputpath-or-inputtransformer)
52-
- [Specifying a Description](#specifying-a-description)
53-
- [Specifying a Name](#specifying-a-name)
54-
- [Specifying a RoleArn](#specifying-a-rolearn)
55-
- [Specifying a custom CloudWatch EventBus](#specifying-a-custom-cloudwatch-eventbus)
56-
- [Specifying a custom EventBridge EventBus](#specifying-a-custom-eventbridge-eventbus)
57-
- [Specifying a DeadLetterQueue](#specifying-a-deadletterqueue)
58-
- [Tags](#tags)
59-
- [Commands](#commands)
60-
- [deploy](#deploy)
61-
- [invoke](#invoke)
62-
- [IAM Role](#iam-role)
63-
- [Tips](#tips)
64-
- [How to specify the stateMachine ARN to environment variables](#how-to-specify-the-statemachine-arn-to-environment-variables)
65-
- [How to split up state machines into files](#how-to-split-up-state-machines-into-files)
66-
- [Sample statemachines setting in serverless.yml](#sample-statemachines-setting-in-serverlessyml)
67-
- [Wait State](#wait-state)
68-
- [Retry Failure](#retry-failure)
69-
- [Parallel](#parallel)
70-
- [Catch Failure](#catch-failure)
71-
- [Choice](#choice)
72-
- [Map](#map)
12+
- [Install](#install)
13+
- [Setup](#Setup)
14+
- [Adding a custom name for a state machine](#adding-a-custom-name-for-a-statemachine)
15+
- [Adding a custom logical id for a stateMachine](#adding-a-custom-logical-id-for-a-statemachine)
16+
- [Depending on another logical id](#depending-on-another-logical-id)
17+
- [Adding retain property for a state machine](#adding-retain-property-for-a-statemachine)
18+
- [CloudWatch Alarms](#cloudwatch-alarms)
19+
- [CloudWatch Notifications](#cloudwatch-notifications)
20+
- [Blue-Green deployments](#blue-green-deployment)
21+
- [Pre-deployment validation](#pre-deployment-validation)
22+
- [Express Workflow](#express-workflow)
23+
- [CloudWatch Logs](#cloudwatch-logs)
24+
- [X-Ray](#x-ray)
25+
- [Current Gotcha](#current-gotcha)
26+
- [Events](#events)
27+
- [API Gateway](#api-gateway)
28+
- [Simple HTTP endpoint](#simple-http-endpoint)
29+
- [Custom Step Functions Action](#custom-step-functions-action)
30+
- [HTTP Endpoint with custom IAM Role](#http-endpoint-with-custom-iam-role)
31+
- [Share API Gateway and API Resources](#share-api-gateway-and-api-resources)
32+
- [Enabling CORS](#enabling-cors)
33+
- [HTTP Endpoints with AWS_IAM Authorizers](#http-endpoints-with-aws_iam-authorizers)
34+
- [HTTP Endpoints with Custom Authorizers](#http-endpoints-with-custom-authorizers)
35+
- [Shared Authorizer](#shared-authorizer)
36+
- [LAMBDA_PROXY request template](#lambda_proxy-request-template)
37+
- [Customizing request body mapping templates](#customizing-request-body-mapping-templates)
38+
- [Customizing response headers and templates](#customizing-response-headers-and-templates)
39+
- [Send request to an API](#send-request-to-an-api)
40+
- [Setting API keys for your Rest API](#setting-api-keys-for-your-rest-api)
41+
- [Request Schema Validators](#request-schema-validators)
42+
- [Schedule](#schedule)
43+
- [Enabling / Disabling](#enabling--disabling)
44+
- [Specify Name and Description](#specify-name-and-description)
45+
- [Scheduled Events IAM Role](#scheduled-events-iam-role)
46+
- [Specify InputTransformer](#specify-inputtransformer)
47+
- [Use EventBridge Scheduler instead of EventBridge rules](#use-eventbridge-scheduler-instead-of-eventbridge-rules)
48+
- [CloudWatch Event](#cloudwatch-event)
49+
- [Simple event definition](#simple-event-definition)
50+
- [Enabling / Disabling](#enabling--disabling-1)
51+
- [Specify Input or Inputpath or InputTransformer](#specify-input-or-inputpath-or-inputtransformer)
52+
- [Specifying a Description](#specifying-a-description)
53+
- [Specifying a Name](#specifying-a-name)
54+
- [Specifying a RoleArn](#specifying-a-rolearn)
55+
- [Specifying a custom CloudWatch EventBus](#specifying-a-custom-cloudwatch-eventbus)
56+
- [Specifying a custom EventBridge EventBus](#specifying-a-custom-eventbridge-eventbus)
57+
- [Specifying a DeadLetterQueue](#specifying-a-deadletterqueue)
58+
- [Tags](#tags)
59+
- [Commands](#commands)
60+
- [deploy](#deploy)
61+
- [invoke](#invoke)
62+
- [IAM Role](#iam-role)
63+
- [Tips](#tips)
64+
- [How to specify the stateMachine ARN to environment variables](#how-to-specify-the-statemachine-arn-to-environment-variables)
65+
- [How to split up state machines into files](#how-to-split-up-state-machines-into-files)
66+
- [Sample statemachines setting in serverless.yml](#sample-statemachines-setting-in-serverlessyml)
67+
- [Wait State](#wait-state)
68+
- [Retry Failure](#retry-failure)
69+
- [Parallel](#parallel)
70+
- [Catch Failure](#catch-failure)
71+
- [Choice](#choice)
72+
- [Map](#map)
7373

7474
## Install
7575

@@ -425,8 +425,8 @@ stepFunctions:
425425
- lambda: LAMBDA_FUNCTION_ARN
426426
- kinesis: KINESIS_STREAM_ARN
427427
- kinesis:
428-
arn: KINESIS_STREAM_ARN
429-
partitionKeyPath: $.id # used to choose the parition key from payload
428+
arn: KINESIS_STREAM_ARN
429+
partitionKeyPath: $.id # used to choose the parition key from payload
430430
- firehose: FIREHOSE_STREAM_ARN
431431
- stepFunctions: STATE_MACHINE_ARN
432432
FAILED:
@@ -790,7 +790,7 @@ stepFunctions:
790790
createUser:
791791
...
792792
events:
793-
- http:
793+
- http:
794794
path: /users
795795
...
796796
authorizer:
@@ -873,6 +873,43 @@ stepFunctions:
873873
definition:
874874
```
875875

876+
If you want to add multiple custom templates for different status codes, headers and content types, you can do so by including them in the `responses` object like so:
877+
878+
```yml
879+
880+
stepFunctions:
881+
stateMachines:
882+
hello:
883+
events:
884+
- http:
885+
path: posts/create
886+
method: POST
887+
responses:
888+
200:
889+
statusCode: 200
890+
responseParameters:
891+
method.response.header.Content-Type: "'application/json'"
892+
method.response.header.X-Application-Id: "'my-app'"
893+
responseTemplates:
894+
application/json: |
895+
{
896+
"status": 200,
897+
"info": "OK"
898+
}
899+
400:
900+
statusCode: 400
901+
responseParameters:
902+
method.response.header.Content-Type: "'application/json'"
903+
method.response.header.X-Application-Id: "'my-app'"
904+
responseTemplates:
905+
application/json: |
906+
{
907+
"status": 400,
908+
"info": "Bad Request"
909+
}
910+
definition:
911+
```
912+
876913
#### Send request to an API
877914

878915
You can input an value as json in request body, the value is passed as the input value of your statemachine
@@ -982,7 +1019,7 @@ provider:
9821019
name: PostCreateModel
9831020
schema: ${file(api_schema/post_add_schema.json)}
9841021
description: "A Model validation for adding posts"
985-
1022+
9861023
stepFunctions:
9871024
stateMachines:
9881025
create:
@@ -1089,10 +1126,10 @@ stepFunctions:
10891126
stateMachines:
10901127
stateMachineScheduled:
10911128
events:
1092-
- schedule:
1129+
- schedule:
10931130
rate: cron(30 12 ? * 1-5 *)
10941131
inputTransformer:
1095-
inputPathsMap:
1132+
inputPathsMap:
10961133
time: '$.time'
10971134
stage: '$.stageVariables'
10981135
inputTemplate: '{"time": <time>, "stage" : <stage> }'
@@ -1424,8 +1461,8 @@ Then
14241461
# to get the Arn of the 1st EventBridge rule
14251462
!GetAtt Hellostepfunc1EventsRuleCloudWatchEvent1.Arn
14261463

1427-
# to get the Arn of the 2nd EventBridge rule
1428-
!GetAtt Hellostepfunc1EventsRuleCloudWatchEvent2.Arn
1464+
# to get the Arn of the 2nd EventBridge rule
1465+
!GetAtt Hellostepfunc1EventsRuleCloudWatchEvent2.Arn
14291466
```
14301467

14311468
## Tags
@@ -1505,12 +1542,12 @@ resources:
15051542
Path: /path_of_state_machine_roles/
15061543
AssumeRolePolicyDocument:
15071544
Statement:
1508-
- Effect: Allow
1509-
Principal:
1510-
Service:
1511-
- states.amazonaws.com
1512-
Action:
1513-
- sts:AssumeRole
1545+
- Effect: Allow
1546+
Principal:
1547+
Service:
1548+
- states.amazonaws.com
1549+
Action:
1550+
- sts:AssumeRole
15141551
Policies:
15151552
- PolicyName: statePolicy
15161553
PolicyDocument:
@@ -1740,21 +1777,21 @@ stepFunctions:
17401777
Type: Parallel
17411778
Next: Final State
17421779
Branches:
1743-
- StartAt: Wait 20s
1744-
States:
1745-
Wait 20s:
1746-
Type: Wait
1747-
Seconds: 20
1748-
End: true
1749-
- StartAt: Pass
1750-
States:
1751-
Pass:
1752-
Type: Pass
1753-
Next: Wait 10s
1754-
Wait 10s:
1755-
Type: Wait
1756-
Seconds: 10
1757-
End: true
1780+
- StartAt: Wait 20s
1781+
States:
1782+
Wait 20s:
1783+
Type: Wait
1784+
Seconds: 20
1785+
End: true
1786+
- StartAt: Pass
1787+
States:
1788+
Pass:
1789+
Type: Pass
1790+
Next: Wait 10s
1791+
Wait 10s:
1792+
Type: Wait
1793+
Seconds: 10
1794+
End: true
17581795
Final State:
17591796
Type: Pass
17601797
End: true
@@ -1782,12 +1819,12 @@ stepFunctions:
17821819
Resource:
17831820
Fn::GetAtt: [hello, Arn]
17841821
Catch:
1785-
- ErrorEquals: ["HandledError"]
1786-
Next: CustomErrorFallback
1787-
- ErrorEquals: ["States.TaskFailed"]
1788-
Next: ReservedTypeFallback
1789-
- ErrorEquals: ["States.ALL"]
1790-
Next: CatchAllFallback
1822+
- ErrorEquals: ["HandledError"]
1823+
Next: CustomErrorFallback
1824+
- ErrorEquals: ["States.TaskFailed"]
1825+
Next: ReservedTypeFallback
1826+
- ErrorEquals: ["States.ALL"]
1827+
Next: CatchAllFallback
17911828
End: true
17921829
CustomErrorFallback:
17931830
Type: Pass
@@ -1834,12 +1871,12 @@ stepFunctions:
18341871
ChoiceState:
18351872
Type: Choice
18361873
Choices:
1837-
- Variable: "$.foo"
1838-
NumericEquals: 1
1839-
Next: FirstMatchState
1840-
- Variable: "$.foo"
1841-
NumericEquals: 2
1842-
Next: SecondMatchState
1874+
- Variable: "$.foo"
1875+
NumericEquals: 1
1876+
Next: FirstMatchState
1877+
- Variable: "$.foo"
1878+
NumericEquals: 2
1879+
Next: SecondMatchState
18431880
Default: DefaultState
18441881
FirstMatchState:
18451882
Type: Task

0 commit comments

Comments
 (0)