Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Overview

SAM is called by the CloudFormation Service. CloudFormation recognises the `Transform: AWS::Serverless-2016-10-31` header and invokes the SAM translator. This will then take your SAM template and expand it
into a full fledged CloudFormation Template. The CloudFormation Template that is produced from SAM is the template that is executed by CloudFormation to create/update/delete AWS resources.

The entry point for SAM starts in the Translator class [here](https://github.com/awslabs/serverless-application-model/blob/develop/samtranslator/translator/translator.py#L29), where SAM iterates through the
template and acts on `AWS::Serverless::*` Type Resources.

# Design decisions

Document design decisions here.
Expand Down
12 changes: 11 additions & 1 deletion DEVELOPMENT_GUIDE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ Tests are also a documentation of the success and failure cases, which is crucia
.. _excellent cheatsheet: http://python-future.org/compatible_idioms.html
.. _pyenv: https://github.com/pyenv/pyenv
.. _tox: http://tox.readthedocs.io/en/latest/
.. _installation instructions: https://github.com/pyenv/pyenv#installation
.. _installation instructions: https://github.com/pyenv/pyenv#installation

Profiling
---------

Install snakeviz `pip install snakeviz`

```
python -m cProfile -o sam_profile_results bin/sam-translate.py translate --input-file=tests/translator/input/alexa_skill.yaml --output-file=cfn-template.json
snakeviz sam_profile_results
```
57 changes: 57 additions & 0 deletions bin/sam-translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python2

"""Convert SAM templates to CloudFormation templates.

Known limitations: cannot transform CodeUri pointing at local directory.

Usage:
sam-translate.py --input-file=sam-template.yaml [--output-file=<o>]

Options:
--input-file=<i> Location of SAM template to transform.
--output-file=<o> Location to store resulting CloudFormation template [default: cfn-template.json].

"""
import json
import os

import boto3
from docopt import docopt

from samtranslator.public.translator import ManagedPolicyLoader
from samtranslator.translator.transform import transform
from samtranslator.yaml_helper import yaml_parse

cli_options = docopt(__doc__)
iam_client = boto3.client('iam')
cwd = os.getcwd()


def get_input_output_file_paths():
input_file_option = cli_options.get('--input-file')
output_file_option = cli_options.get('--output-file')
input_file_path = os.path.join(cwd, input_file_option)
output_file_path = os.path.join(cwd, output_file_option)

return input_file_path, output_file_path


def main():
input_file_path, output_file_path = get_input_output_file_paths()

with open(input_file_path, 'r') as f:
sam_template = yaml_parse(f)

cloud_formation_template = transform(
sam_template, {}, ManagedPolicyLoader(iam_client))
cloud_formation_template_prettified = json.dumps(
cloud_formation_template, indent=2)

with open(output_file_path, 'w') as f:
f.write(cloud_formation_template_prettified)

print('Wrote transformed CloudFormation template to: ' + output_file_path)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions docs/cloudformation_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Kinesis
Property Name Intrinsic(s) Supported Reasons
======================== ================================== ========================
Stream All
Queue All
StartingPosition All
BatchSize All
======================== ================================== ========================
Expand Down
27 changes: 27 additions & 0 deletions docs/internals/generated_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,33 @@ AWS::Lambda::Permissions MyFunction\ **MyTrigger**\ Permission
AWS::Lambda::EventSourceMapping MyFunction\ **MyTrigger**
================================== ================================

SQS
^^^^^^^

Example:

.. code:: yaml

MyFunction:
Type: AWS::Serverless::Function
Properties:
...
Events:
MyTrigger:
Type: SQS
Properties:
Queue: arn:aws:sqs:us-east-1:123456789012:my-queue
...

Additional generated resources:

================================== ================================
CloudFormation Resource Type Logical ID
================================== ================================
AWS::Lambda::Permissions MyFunction\ **MyTrigger**\ Permission
AWS::Lambda::EventSourceMapping MyFunction\ **MyTrigger**
================================== ================================

DynamoDb
^^^^^^^^

Expand Down
8 changes: 1 addition & 7 deletions examples/2016-10-31/api_backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ const dynamo = new AWS.DynamoDB.DocumentClient();

const tableName = process.env.TABLE_NAME;

const createResponse = (statusCode, body) => {

return {
statusCode: statusCode,
body: body
}
};
const createResponse = (statusCode, body) => ({ statusCode, body });

exports.get = (event, context, callback) => {

Expand Down
1 change: 1 addition & 0 deletions examples/2016-10-31/sqs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
transformed-cfn-template.yaml
19 changes: 19 additions & 0 deletions examples/2016-10-31/sqs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SQS Event Source Example

Example SAM template for processing messages on an SQS queue.

## Running the example

```bash
# Set YOUR_S3_ARTIFACTS_BUCKET to a bucket you own
YOUR_S3_ARTIFACTS_BUCKET='YOUR_S3_ARTIFACTS_BUCKET'; \
aws cloudformation package --template-file template.yaml --output-template-file cfn-transformed-template.yaml --s3-bucket $YOUR_S3_ARTIFACTS_BUCKET
aws cloudformation deploy --template-file ./cfn-transformed-template.yaml --stack-name lambda-sqs-processor --capabilities CAPABILITY_IAM
```

After your CloudFormation Stack has completed creation, send a message to the SQS queue to see it in action:

```bash
YOUR_SQS_QUEUE_URL=https://sqs.us-east-1.amazonaws.com/123456789012/my-queue; \
aws sqs send-message --queue-url $YOUR_SQS_QUEUE_URL --message-body '{ "myMessage": "Hello SAM!" }'
```
10 changes: 10 additions & 0 deletions examples/2016-10-31/sqs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
async function handler (event, context) {
// TODO: Handle message...
const records = event.Records

console.log(records)

return {}
}

module.exports.handler = handler
19 changes: 19 additions & 0 deletions examples/2016-10-31/sqs/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example of processing messages on an SQS queue with Lambda
Resources:
MySQSQueueFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./index.js
Handler: index.handler
Runtime: nodejs8.10
Events:
MySQSEvent:
Type: SQS
Properties:
Queue: !GetAtt MySqsQueue.Arn

MySqsQueue:
Type: AWS::SQS::Queue
Properties:
167 changes: 0 additions & 167 deletions examples/apps/microservice-http-endpoint-python3/index.js

This file was deleted.

Loading