Skip to content

Commit 8cca3d5

Browse files
committed
Specification changes and examples for new features
1. Inline Swagger 2. CodeUri & DefinitionUri support Body, Key and Version dictionary 3. FunctionName property
1 parent 33194de commit 8cca3d5

File tree

5 files changed

+139
-3
lines changed

5 files changed

+139
-3
lines changed

HOWTO.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,71 @@ aws cloudformation deploy \
8181
```
8282

8383
Refer to the [documentation](http://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html) for more details.
84+
85+
## Using Intrinsic Functions
86+
CloudFormation provides handy functions you can use to generate values at runtime. These are called [Intrinsic Functions](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html). Since SAM is deployed using CloudFormation, you can use these intrinsic functions within SAM as well. Here are some examples:
87+
88+
#### Dynamically set S3 location of Lambda function code zip
89+
```
90+
Transform: 'AWS::Serverless-2016-10-31'
91+
92+
# Parameters are CloudFormation features to pass input
93+
# to your template when you create a stack
94+
Parameters:
95+
BucketName:
96+
Type: String
97+
CodeKey:
98+
Type: String
99+
100+
Resources:
101+
MyFunction:
102+
Type: AWS::Serverless::Function
103+
Properties:
104+
Handler: index.handler
105+
Runtime: nodejs4.3
106+
CodeUri:
107+
# !Ref function allows you to fetch value
108+
# of parameters and other resources at runtime
109+
Bucket: !Ref BucketName
110+
Key: !Ref CodeKey
111+
```
112+
113+
#### Generate a different function name for each stack
114+
115+
```
116+
Transform: 'AWS::Serverless-2016-10-31'
117+
118+
# Parameters are CloudFormation features to pass input
119+
# to your template when you create a stack
120+
Parameters:
121+
FunctionNameSuffix:
122+
Type: String
123+
124+
Resources:
125+
MyFunction:
126+
Type: AWS::Serverless::Function
127+
Properties:
128+
129+
# !Sub performs string substitution
130+
FunctionName: !Sub "mylambda-${FunctionNameSuffix}"
131+
132+
Handler: index.handler
133+
Runtime: nodejs4.3
134+
CodeUri: s3://bucket/key
135+
```
136+
137+
### Caveats:
138+
#### ImportValue is partially supported
139+
[`ImportValue`](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html) allows one stack to refer to value of properties from another stack. ImportValue is supported on most properties, except the very few that SAM needs to parse. Following properties are *not* supported:
140+
141+
- `RestApiId` of `AWS::Serverless::Function`
142+
- `Policies` of `AWS::Serverless::Function`
143+
- `StageName` of `AWS::Serverless::Api`
144+
145+
146+
147+
148+
149+
150+
151+
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
AWSTemplateFormatVersion : '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: A hello world application.
4+
Parameters:
5+
Bucket:
6+
Type: String
7+
CodeZipKey:
8+
Type: String
49
Resources:
510
HelloWorldFunction:
611
Type: AWS::Serverless::Function
712
Properties:
813
Handler: index.handler
914
Runtime: nodejs4.3
10-
CodeUri: s3://<bucket>/hello_world.zip
15+
CodeUri:
16+
Bucket: !Bucket
17+
Key: !CodeZipKey
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
exports.handler = function(event, context, callback) {
2+
callback(null, {
3+
"statusCode": 200,
4+
"body": "hello world"
5+
});
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: Simple API Endpoint configured using Swagger specified inline and backed by a Lambda function
4+
Resources:
5+
MyApi:
6+
Type: AWS::Serverless::Api
7+
Properties:
8+
StageName: prod
9+
DefinitionBody:
10+
swagger: 2.0
11+
info:
12+
title:
13+
Ref: AWS::StackName
14+
paths:
15+
"/get":
16+
get:
17+
x-amazon-apigateway-integration:
18+
httpMethod: POST
19+
type: aws_proxy
20+
uri:
21+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambdaFunction.Arn}/invocations
22+
responses: {}
23+
swagger: '2.0'
24+
25+
26+
27+
MyLambdaFunction:
28+
Type: AWS::Serverless::Function
29+
Properties:
30+
Handler: index.handler
31+
Runtime: nodejs4.3
32+
CodeUri: s3://<bucket>/inline_swagger.zip
33+
Events:
34+
GetApi:
35+
Type: Api
36+
Properties:
37+
Path: /get
38+
Method: GET
39+
RestApiId:
40+
Ref: MyApi
41+

versions/2016-10-31.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ Property Name | Type | Description
6262
---|:---:|---
6363
Handler | `string` | **Required.** Function within your code that is called to begin execution.
6464
Runtime | `string` | **Required.** The runtime environment.
65-
CodeUri | `string` | **Required.** S3 Uri to the function code. The S3 object this Uri references MUST be a [Lambda deployment package](http://docs.aws.amazon.com/lambda/latest/dg/deployment-package-v2.html).
65+
CodeUri | `string` <span>&#124;</span> [S3 Location Object](#s3-location-object) | **Required.** S3 Uri or location to the function code. The S3 object this Uri references MUST be a [Lambda deployment package](http://docs.aws.amazon.com/lambda/latest/dg/deployment-package-v2.html).
66+
FunctionName | `string` | A name for the function. If you don't specify a name, a unique name will be generated for you. [More Info](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-functionname)
6667
Description | `string` | Description of the function.
6768
MemorySize | `integer` | Size of the memory allocated per invocation of the function in MB. Defaults to 128.
6869
Timeout | `integer` | Maximum time that the function can run before it is killed in seconds. Defaults to 3.
@@ -117,7 +118,8 @@ An `AWS::Serverless::Api` resource need not be explicitly added to a AWS Serverl
117118
Property Name | Type | Description
118119
---|:---:|---
119120
StageName | `string` | **Required** The name of the stage, which API Gateway uses as the first path segment in the invoke Uniform Resource Identifier (URI).
120-
DefinitionUri | `string` | **Required** S3 URI to the Swagger document describing the API.
121+
DefinitionUri | `string` <span>&#124;</span> [S3 Location Object](#s3-location-object) | S3 URI or location to the Swagger document describing the API. Either one of `DefinitionUri` or `DefinitionBody` must be specified.
122+
DefinitionBody | `JSON or YAML Object` | Swagger specification that describes your API. Either one of `DefinitionUri` or `DefinitionBody` must be specified.
121123
CacheClusterEnabled | `boolean` | Indicates whether cache clustering is enabled for the stage.
122124
CacheClusterSize | `string` | The stage's cache cluster size.
123125
Variables | Map of `string` to `string` | A map (string to string map) that defines the stage variables, where the variable name is the key and the variable value is the value. Variable names are limited to alphanumeric characters. Values must match the following regular expression: `[A-Za-z0-9._~:/?#&amp;=,-]+`.
@@ -396,3 +398,15 @@ Type | `string` | Attribute type of the primary key. MUST be one of `String`, `N
396398
Name: id
397399
Type: String
398400
```
401+
402+
### Data Types
403+
#### S3 Location Object
404+
Specifies the location of an S3 object as a dictionary containing `Bucket`, `Key`, and optional `Version` properties.
405+
406+
Example:
407+
```
408+
CodeUri:
409+
Bucket: mybucket-name
410+
Key: code.zip
411+
Version: 121212
412+
```

0 commit comments

Comments
 (0)