Skip to content

Commit

Permalink
Specification changes and examples for new features
Browse files Browse the repository at this point in the history
1. Inline Swagger
2. CodeUri & DefinitionUri support Body, Key and Version dictionary
3. FunctionName property
  • Loading branch information
sanathkr committed Feb 17, 2017
1 parent 33194de commit 8cca3d5
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
68 changes: 68 additions & 0 deletions HOWTO.md
Expand Up @@ -81,3 +81,71 @@ aws cloudformation deploy \
```

Refer to the [documentation](http://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html) for more details.

## Using Intrinsic Functions
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:

#### Dynamically set S3 location of Lambda function code zip
```
Transform: 'AWS::Serverless-2016-10-31'
# Parameters are CloudFormation features to pass input
# to your template when you create a stack
Parameters:
BucketName:
Type: String
CodeKey:
Type: String
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs4.3
CodeUri:
# !Ref function allows you to fetch value
# of parameters and other resources at runtime
Bucket: !Ref BucketName
Key: !Ref CodeKey
```

#### Generate a different function name for each stack

```
Transform: 'AWS::Serverless-2016-10-31'
# Parameters are CloudFormation features to pass input
# to your template when you create a stack
Parameters:
FunctionNameSuffix:
Type: String
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
# !Sub performs string substitution
FunctionName: !Sub "mylambda-${FunctionNameSuffix}"
Handler: index.handler
Runtime: nodejs4.3
CodeUri: s3://bucket/key
```

### Caveats:
#### ImportValue is partially supported
[`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:

- `RestApiId` of `AWS::Serverless::Function`
- `Policies` of `AWS::Serverless::Function`
- `StageName` of `AWS::Serverless::Api`








9 changes: 8 additions & 1 deletion examples/2016-10-31/hello_world/template.yaml
@@ -1,10 +1,17 @@
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A hello world application.
Parameters:
Bucket:
Type: String
CodeZipKey:
Type: String
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs4.3
CodeUri: s3://<bucket>/hello_world.zip
CodeUri:
Bucket: !Bucket
Key: !CodeZipKey
6 changes: 6 additions & 0 deletions examples/2016-10-31/inline_swagger/index.js
@@ -0,0 +1,6 @@
exports.handler = function(event, context, callback) {
callback(null, {
"statusCode": 200,
"body": "hello world"
});
}
41 changes: 41 additions & 0 deletions examples/2016-10-31/inline_swagger/template.yaml
@@ -0,0 +1,41 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Simple API Endpoint configured using Swagger specified inline and backed by a Lambda function
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: 2.0
info:
title:
Ref: AWS::StackName
paths:
"/get":
get:

This comment has been minimized.

Copy link
@Dunedan

Dunedan Feb 21, 2017

Contributor

Is this duplicated notation of get intentional?

This comment has been minimized.

Copy link
@sanathkr

sanathkr Feb 21, 2017

Author Contributor

Nope.. One is the API path, other is the method.

x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambdaFunction.Arn}/invocations
responses: {}
swagger: '2.0'

This comment has been minimized.

Copy link
@jed

jed Feb 21, 2017

swagger appears twice in this DefinitionBody

This comment has been minimized.

Copy link
@sanathkr

sanathkr Feb 21, 2017

Author Contributor

Thanks, need to remove one

This comment has been minimized.

Copy link
@philipakash

philipakash Feb 21, 2017

but the intrinsic functions are still not getting resolved in external swagger files. Is this just for inline swagger {$AWS:Region}
The Stage Variable is not catching up the account id just like Lambda Function

This comment has been minimized.

Copy link
@sanathkr

sanathkr Feb 21, 2017

Author Contributor

CloudFormation does not support intrinsics on external Swagger. About the AccountId, check out - #87. This is an API Gateway limitation unfortunately. Your best bet is to inline your Swagger and enjoy all benefits

This comment has been minimized.

Copy link
@philipakash

philipakash Feb 22, 2017

Also When we use inline swagger do we need to make some change in the package style?
coz I get everything correct but after packaging, my template file is getting populated with definitionUri as well which is throwing error that either definitionBody or definitionUri should be there in the template file.(i just added definitionBody but don't know how the other field is coming in)
Am i doing something wrong ?

This comment has been minimized.

Copy link
@jed

jed Feb 22, 2017

@philipakash, i ran into the same thing. the fix hasn't landed yet, so for now i just run sed -i '' '/DefinitionUri/d' packaged-template.yaml (macOS) after aws cloudformation package.

This comment has been minimized.

Copy link
@philipakash

philipakash Feb 22, 2017

hope we get this fix soon :-) !! Thanks @jed




MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs4.3
CodeUri: s3://<bucket>/inline_swagger.zip
Events:
GetApi:
Type: Api
Properties:
Path: /get
Method: GET
RestApiId:
Ref: MyApi

18 changes: 16 additions & 2 deletions versions/2016-10-31.md
Expand Up @@ -62,7 +62,8 @@ Property Name | Type | Description
---|:---:|---
Handler | `string` | **Required.** Function within your code that is called to begin execution.
Runtime | `string` | **Required.** The runtime environment.
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).
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).
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)
Description | `string` | Description of the function.
MemorySize | `integer` | Size of the memory allocated per invocation of the function in MB. Defaults to 128.
Timeout | `integer` | Maximum time that the function can run before it is killed in seconds. Defaults to 3.
Expand Down Expand Up @@ -117,7 +118,8 @@ An `AWS::Serverless::Api` resource need not be explicitly added to a AWS Serverl
Property Name | Type | Description
---|:---:|---
StageName | `string` | **Required** The name of the stage, which API Gateway uses as the first path segment in the invoke Uniform Resource Identifier (URI).
DefinitionUri | `string` | **Required** S3 URI to the Swagger document describing the API.
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.
DefinitionBody | `JSON or YAML Object` | Swagger specification that describes your API. Either one of `DefinitionUri` or `DefinitionBody` must be specified.
CacheClusterEnabled | `boolean` | Indicates whether cache clustering is enabled for the stage.
CacheClusterSize | `string` | The stage's cache cluster size.
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;=,-]+`.
Expand Down Expand Up @@ -396,3 +398,15 @@ Type | `string` | Attribute type of the primary key. MUST be one of `String`, `N
Name: id
Type: String
```

### Data Types
#### S3 Location Object
Specifies the location of an S3 object as a dictionary containing `Bucket`, `Key`, and optional `Version` properties.

Example:
```
CodeUri:
Bucket: mybucket-name
Key: code.zip
Version: 121212
```

0 comments on commit 8cca3d5

Please sign in to comment.