Skip to content

Commit 629ddf2

Browse files
committed
New Features for API, Function & SimpleTable # 248, #252, #265
* API * CORS * Regional endpoints * Binary media types * Logging, Metrics & CacheTTL (MethodSettings) * Function * Per-Function Concurrency (ReservedConcurrentExecutions) * SimpleTable: * Tags * TableName * Refer to resources generated by API: * `!Ref MyApi.Stage` => Ref the Stage generated by SAM * `!Ref MyApi.Deployment` => Ref the Deployment resource generated by SAM
1 parent 9b15248 commit 629ddf2

File tree

7 files changed

+220
-72
lines changed

7 files changed

+220
-72
lines changed

docs/globals.rst

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Globals Section
33

44
.. contents::
55

6-
Lambda functions within a SAM template tend to have shared configuration such as Runtime, Memory,
7-
VPC Settings, Environment Variables etc. Instead of duplicating this information in every function, you can
8-
write them once in the ``Globals`` section and let all Functions inhert it.
6+
Resources in a SAM template tend to have shared configuration such as Runtime, Memory,
7+
VPC Settings, Environment Variables, Cors etc. Instead of duplicating this information in every resource, you can
8+
write them once in the ``Globals`` section and let all resources inhert it.
99

1010
Example:
1111

@@ -44,23 +44,65 @@ inherited TABLE_NAME. ``ThumbnailFunction`` inherits all the Globals properties
4444

4545
Supported Resources
4646
-------------------
47-
Properties of ``AWS::Serverless::Function`` are only supported in Globals section presently.
47+
Properties of ``AWS::Serverless::Function`` and ``AWS::Serverless::Api`` are only supported in Globals section
48+
presently.
4849

4950
.. code:: yaml
5051
5152
Globals:
5253
Function:
53-
# Properties of AWS::Serverless::Function
54-
55-
Following properties of ``AWS::Serverless::Function`` are **not** supported in Globals section. We made the explicitly
54+
# Some properties of AWS::Serverless::Function
55+
Handler:
56+
Runtime:
57+
CodeUri:
58+
DeadLetterQueue:
59+
Description:
60+
MemorySize:
61+
Timeout:
62+
VpcConfig:
63+
Environment:
64+
Tags:
65+
Tracing:
66+
KmsKeyArn:
67+
AutoPublishAlias:
68+
DeploymentPreference:
69+
70+
Api:
71+
# Some properties of AWS::Serverless::Api
72+
# Also works with Implicit APIs
73+
Name:
74+
DefinitionUri:
75+
CacheClusterEnabled:
76+
CacheClusterSize:
77+
Variables:
78+
EndpointConfiguration:
79+
MethodSettings:
80+
BinaryMediaTypes:
81+
Cors:
82+
83+
Implicit APIs
84+
~~~~~~~~~~~~~
85+
86+
APIs created by SAM when you have an API declared in the ``Events`` section are called "Implicit APIs". You can use
87+
Globals to override all properties of Implicit APIs as well.
88+
89+
Unsupported Properties
90+
~~~~~~~~~~~~~~~~~~~~~~
91+
92+
Following properties of are **not** supported in Globals section. We made the explicitly
5693
call to not support them because it either made the template hard to understand or opens scope for potential security
5794
issues.
5895

96+
**AWS::Serverless::Function:**
5997
* Role
6098
* Policies
6199
* FunctionName
62100
* Events
63101

102+
**AWS::Serverless::Api:**
103+
* StageName
104+
* DefinitionBody
105+
64106
Overridable
65107
-----------
66108

@@ -148,7 +190,3 @@ SecurityGroupIds of VpcConfig will be set to ``["sg-first", "sg-123", "sg-456"]`
148190
SecurityGroupIds:
149191
- sg-first
150192
151-
152-
153-
154-

examples/2016-10-31/api_swagger_cors/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ exports.handler = function(event, context, callback) {
22

33
callback(null, {
44
statusCode: '200',
5-
body: "Hello world"
5+
body: "Hello world",
6+
headers: {
7+
// This is ALSO required for CORS to work. When browsers issue cross origin requests, they make a
8+
// preflight request (HTTP Options) which is responded automatically based on SAM configuration.
9+
// But the actual HTTP request (GET/POST etc) also needs to contain the AllowOrigin header.
10+
//
11+
// NOTE: This value is *not* double quoted: ie. "'www.example.com'" is wrong
12+
"Access-Control-Allow-Origin": "https://www.example.com"
13+
}
614
});
715

816
}

examples/2016-10-31/api_swagger_cors/swagger.yaml

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,6 @@ paths:
2626
passthroughBehavior: when_no_match
2727
httpMethod: POST
2828
type: aws_proxy
29-
options:
30-
consumes:
31-
- application/json
32-
produces:
33-
- application/json
34-
responses:
35-
'200':
36-
description: 200 response
37-
schema:
38-
$ref: "#/definitions/Empty"
39-
headers:
40-
Access-Control-Allow-Origin:
41-
type: string
42-
Access-Control-Allow-Methods:
43-
type: string
44-
Access-Control-Allow-Headers:
45-
type: string
46-
x-amazon-apigateway-integration:
47-
responses:
48-
default:
49-
statusCode: 200
50-
responseParameters:
51-
method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
52-
method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
53-
method.response.header.Access-Control-Allow-Origin: "'*'"
54-
passthroughBehavior: when_no_match
55-
requestTemplates:
56-
application/json: "{\"statusCode\": 200}"
57-
type: mock
5829
/{proxy+}:
5930
x-amazon-apigateway-any-method:
6031
x-amazon-apigateway-auth:
@@ -72,35 +43,6 @@ paths:
7243
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations"
7344
httpMethod: POST
7445
type: aws_proxy
75-
options:
76-
consumes:
77-
- application/json
78-
produces:
79-
- application/json
80-
responses:
81-
'200':
82-
description: 200 response
83-
schema:
84-
$ref: "#/definitions/Empty"
85-
headers:
86-
Access-Control-Allow-Origin:
87-
type: string
88-
Access-Control-Allow-Methods:
89-
type: string
90-
Access-Control-Allow-Headers:
91-
type: string
92-
x-amazon-apigateway-integration:
93-
responses:
94-
default:
95-
statusCode: 200
96-
responseParameters:
97-
method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
98-
method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
99-
method.response.header.Access-Control-Allow-Origin: "'*'"
100-
passthroughBehavior: when_no_match
101-
requestTemplates:
102-
application/json: "{\"statusCode\": 200}"
103-
type: mock
10446
definitions:
10547
Empty:
10648
type: object

examples/2016-10-31/api_swagger_cors/template.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Resources:
77
Type: AWS::Serverless::Api
88
Properties:
99
StageName: Prod
10+
11+
# Allows www.example.com to call these APIs
12+
# SAM will automatically add AllowMethods with a list of methods for this API
13+
Cors: "'www.example.com'"
14+
1015
DefinitionBody:
1116
'Fn::Transform':
1217
Name: 'AWS::Include'

examples/2016-10-31/implicit_api_settings/index.js

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
Transform: AWS::Serverless-2016-10-31
3+
4+
Globals:
5+
Api:
6+
# Allows www.example.com to call these APIs
7+
# SAM will automatically add AllowMethods with a list of methods for this API
8+
Cors: "'https://www.www.example.com'"
9+
10+
# API Gateway regional endpoints
11+
EndpointConfiguration: REGIONAL
12+
13+
# Send/receive binary data through the APIs
14+
BinaryMediaTypes:
15+
# This is equivalent to image/gif when deployed
16+
- image~1gif
17+
- iimage~1png
18+
19+
# Logging, Metrics, Throttling, and all other Stage settings
20+
MethodSettings: [{
21+
# Turn on Info logging
22+
"LoggingLevel": "INFO",
23+
24+
# Enable Metrics
25+
"MetricsEnabled": True,
26+
27+
# Trace-level Logging
28+
"DataTraceEnabled": True,
29+
30+
# On all Paths & methods
31+
"ResourcePath": "/*",
32+
"HttpMethod": "*",
33+
}]
34+
35+
Resources:
36+
LambdaFunction:
37+
Type: AWS::Serverless::Function
38+
Properties:
39+
# Replace <bucket> with your bucket name
40+
CodeUri: s3://<bucket>/code.zip
41+
Handler: index.handler
42+
Runtime: nodejs6.10
43+
Events:
44+
ProxyApiRoot:
45+
Type: Api
46+
Properties:
47+
Path: /
48+
Method: ANY
49+
ProxyApiGreedy:
50+
Type: Api
51+
Properties:
52+
Path: /{proxy+}
53+
Method: ANY
54+
55+
####### Necessary for API Gateway Logging ########
56+
# Add the CloudWatchRole and Account resource to your template to give API Gateway permissions write to CloudWatch logs
57+
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-account.html#aws-resource-apigateway-account-examples
58+
#
59+
# NOTE: This is a one time process. As long as you have this enabled once in a region, you can deploy other stacks
60+
# without the need for each stack to create this role. As a good practice, create a separate stack altogether
61+
# with just the API Gateway logging role so none of your application stacks need them.
62+
ApiGwAccountConfig:
63+
Type: "AWS::ApiGateway::Account"
64+
Properties:
65+
CloudWatchRoleArn: !GetAtt "ApiGatewayLoggingRole.Arn"
66+
ApiGatewayLoggingRole:
67+
Type: "AWS::IAM::Role"
68+
Properties:
69+
AssumeRolePolicyDocument:
70+
Version: "2012-10-17"
71+
Statement:
72+
- Effect: Allow
73+
Principal:
74+
Service:
75+
- "apigateway.amazonaws.com"
76+
Action: "sts:AssumeRole"
77+
Path: "/"
78+
ManagedPolicyArns:
79+
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
80+
81+
Outputs:
82+
ApiUrl:
83+
Description: URL of your API endpoint
84+
Value: !Join
85+
- ''
86+
- - https://
87+
- !Ref ServerlessRestApi
88+
- '.execute-api.'
89+
- !Ref 'AWS::Region'
90+
- '.amazonaws.com/Prod'

0 commit comments

Comments
 (0)