Skip to content

Commit

Permalink
chore: merge release/v1.10.0 into master
Browse files Browse the repository at this point in the history
Merge pull request #843 from awslabs/release/v1.10.0
  • Loading branch information
brettstack authored Mar 6, 2019
2 parents 7c621f2 + 8116967 commit 805cc85
Show file tree
Hide file tree
Showing 234 changed files with 23,157 additions and 1,851 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 120
ignore = E126
ignore = E126 F821 W504 W605
30 changes: 15 additions & 15 deletions DEVELOPMENT_GUIDE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,26 @@ 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
```
.. code-block:: shell
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
Verifying transforms
--------------------

If you make changes to the transformer and want to verify the resulting CloudFormation template works as expected, you can transform your SAM template into a CloudFormation template using the following process:

```shell
# Optional: You only need to run the package command in certain cases; e.g. when your CodeUri specifies a local path
# Replace MY_TEMPLATE_PATH with the path to your template and MY_S3_BUCKET with an existing S3 bucket
aws cloudformation package --template-file MY_TEMPLATE_PATH/template.yaml --output-template-file output-template.yaml --s3-bucket MY_S3_BUCKET
.. code-block:: shell
# Optional: You only need to run the package command in certain cases; e.g. when your CodeUri specifies a local path
# Replace MY_TEMPLATE_PATH with the path to your template and MY_S3_BUCKET with an existing S3 bucket
aws cloudformation package --template-file MY_TEMPLATE_PATH/template.yaml --output-template-file output-template.yaml --s3-bucket MY_S3_BUCKET
# Transform your SAM template into a CloudFormation template
# Replace "output-template.yaml" if you didn't run the package command above or specified a different path for --output-template-file
bin/sam-translate.py --input-file=output-template.yaml
# Transform your SAM template into a CloudFormation template
# Replace "output-template.yaml" if you didn't run the package command above or specified a different path for --output-template-file
bin/sam-translate.py --input-file=output-template.yaml
# Deploy your transformed CloudFormation template
# Replace MY_STACK_NAME with a unique name each time you deploy
aws cloudformation deploy --template-file cfn-template.json --capabilities CAPABILITY_NAMED_IAM --stack-name MY_STACK_NAME
```
# Deploy your transformed CloudFormation template
# Replace MY_STACK_NAME with a unique name each time you deploy
aws cloudformation deploy --template-file cfn-template.json --capabilities CAPABILITY_NAMED_IAM --stack-name MY_STACK_NAME
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ init:
$(info [*] Install requirements...)
@pip install -r requirements/dev.txt -r requirements/base.txt

flake:
$(info [*] Running flake8...)
@flake8 samtranslator

test:
$(info [*] Run the unit test with minimum code coverage of $(CODE_COVERAGE)%...)
@pytest --cov samtranslator --cov-report term-missing --cov-fail-under $(CODE_COVERAGE) tests
Expand All @@ -49,7 +53,7 @@ build-docs:
@$(MAKE) -C docs/website html

# Command to run everytime you make changes to verify everything works
dev: test
dev: flake test

# Verifications to run before sending a pull request
pr: init dev
Expand All @@ -68,4 +72,4 @@ TARGETS
build-docs Generate the documentation.
pr Perform all checks before submitting a Pull Request.

endef
endef
2 changes: 2 additions & 0 deletions docs/cloudformation_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ Variables All
EndpointConfiguration All
MethodSettings All
BinaryMediaTypes All
MinimumCompressionSize All
Cors All
TracingEnabled All
================================== ======================== ========================


Expand Down
15 changes: 10 additions & 5 deletions docs/globals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ Example:
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Environment:
Variables:
MESSAGE: "Hello From SAM"
Properties:
Environment:
Variables:
MESSAGE: "Hello From SAM"
ThumbnailFunction:
Type: AWS::Serverless::Function
Expand Down Expand Up @@ -53,7 +54,6 @@ Currently, the following resources and properties are being supported:
# Properties of AWS::Serverless::Function
Handler:
Runtime:
# Specifying CodeUri on Globals is not yet supported by 'CloudFormation package' https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html
CodeUri:
DeadLetterQueue:
Description:
Expand All @@ -67,7 +67,8 @@ Currently, the following resources and properties are being supported:
Layers:
AutoPublishAlias:
DeploymentPreference:
PermissionsBoundary:
Api:
# Properties of AWS::Serverless::Api
# Also works with Implicit APIs
Expand All @@ -79,7 +80,11 @@ Currently, the following resources and properties are being supported:
EndpointConfiguration:
MethodSettings:
BinaryMediaTypes:
MinimumCompressionSize:
Cors:
AccessLogSetting:
CanarySetting:
TracingEnabled:
SimpleTable:
# Properties of AWS::Serverless::SimpleTable
Expand Down
4 changes: 4 additions & 0 deletions examples/2016-10-31/api_cognito_auth/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Resources:
RestApiId: !Ref MyApi
Path: /
Method: GET
# NOTE: This endpoint is publicly accessible
Auth:
Authorizer: NONE
ProxyAny:
Expand All @@ -48,6 +49,7 @@ Resources:
RestApiId: !Ref MyApi
Path: /{proxy+}
Method: ANY
# NOTE: This endpoint is publicly accessible
Auth:
Authorizer: NONE
GetUsers:
Expand All @@ -56,6 +58,7 @@ Resources:
RestApiId: !Ref MyApi
Path: /users
Method: GET
# NOTE: This endpoint is publicly accessible
Auth:
Authorizer: NONE
GetUser:
Expand All @@ -64,6 +67,7 @@ Resources:
RestApiId: !Ref MyApi
Path: /users/{userId}
Method: GET
# NOTE: This endpoint is publicly accessible
Auth:
Authorizer: NONE
CreateUser:
Expand Down
9 changes: 8 additions & 1 deletion examples/2016-10-31/api_lambda_request_auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The Authorizer Lambda Function in this example simply accepts an `auth` query st

## Running the example

Optional: Uncomment the following lines in `template.yaml` to enable a publicly accessible endpoint:

```yaml
# Auth:
# Authorizer: NONE
```

Deploy the example into your account:

```bash
Expand All @@ -15,7 +22,7 @@ aws cloudformation package --template-file template.yaml --output-template-file
aws cloudformation deploy --template-file ./template.packaged.yaml --stack-name sam-example-api-lambda-request-auth --capabilities CAPABILITY_IAM
```

Invoke the API's root endpoint `/` without an `auth` query string to see the API respond with a 200. In the SAM template, we explicitly state `Authorizer: NONE` to make this a public/open endpoint (the Authorizer Lambda Function is not invoked).
Invoke the API's root endpoint `/` without an `auth` query string to see the API respond with a 200 (assuming you followed the optional step above). In the SAM template, we explicitly state `Authorizer: NONE` to make this a public/open endpoint (the Authorizer Lambda Function is not invoked).

```bash
api_url=$(aws cloudformation describe-stacks --stack-name sam-example-api-lambda-request-auth --query 'Stacks[].Outputs[?OutputKey==`ApiURL`].OutputValue' --output text)
Expand Down
5 changes: 3 additions & 2 deletions examples/2016-10-31/api_lambda_request_auth/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ Resources:
RestApiId: !Ref MyApi
Path: /
Method: get
Auth:
Authorizer: NONE
# NOTE: Uncomment the two lines below to make `GET /` publicly accessible
# Auth:
# Authorizer: NONE
GetUsers:
Type: Api
Properties:
Expand Down
9 changes: 8 additions & 1 deletion examples/2016-10-31/api_lambda_token_auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The Authorizer Lambda Function in this example simply accepts an `Authorization`

## Running the example

Optional: Uncomment the following lines in `template.yaml` to enable a publicly accessible endpoint:

```yaml
# Auth:
# Authorizer: NONE
```

Deploy the example into your account:

```bash
Expand All @@ -15,7 +22,7 @@ aws cloudformation package --template-file template.yaml --output-template-file
aws cloudformation deploy --template-file ./template.packaged.yaml --stack-name sam-example-api-lambda-token-auth --capabilities CAPABILITY_IAM
```

Invoke the API's root endpoint `/` without an `Authorization` header to see the API respond with a 200. In the SAM template, we explicitly state `Authorizer: NONE` to make this a public/open endpoint (the Authorizer Lambda Function is not invoked).
Invoke the API's root endpoint `/` without an `Authorization` header to see the API respond with a 200 (assuming you followed the optional step above). In the SAM template, we explicitly state `Authorizer: NONE` to make this a public/open endpoint (the Authorizer Lambda Function is not invoked).

```bash
curl "$(aws cloudformation describe-stacks --stack-name sam-example-api-lambda-token-auth --query 'Stacks[].Outputs[?OutputKey==`ApiURL`].OutputValue' --output text)"
Expand Down
5 changes: 3 additions & 2 deletions examples/2016-10-31/api_lambda_token_auth/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Resources:
RestApiId: !Ref MyApi
Path: /
Method: get
Auth:
Authorizer: NONE
# NOTE: Uncomment the two lines below to make `GET /` publicly accessible
# Auth:
# Authorizer: NONE
GetUsers:
Type: Api
Properties:
Expand Down
27 changes: 27 additions & 0 deletions examples/2016-10-31/image_resize_golang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# AWS::Serverless::S3 Event Code Example
This example shows you how to get events of S3 bucket. When you upload an image in the source bucket it will resize that image and save in the destination bucket.
###### Note
Don't forget to chnage S3 bucket name, source bucket prefix.

## Package and deploy template
#### Build
Run the following command to build binary of your code
```
GOOS=linux go build -o main
```
#### Package
To package your application run the following command
```
sam package --template-file template.yaml --output-template-file serverless-output.yaml --s3-bucket <YOUR_S3_BUCKET>
```
It will validate the template, zip you applicaiton, upload to your S3 bucket and generates the output template. Replace the `<YOUR_S3_BUCKET>` with your bucket name.
#### Deploy
Run the following command replacing `<BUCKET_PREFIX>` with your desire prefix name to deploye your application
```
sam deploy --template-file serverless-output.yaml --stack-name image-resizer --capabilities CAPABILITY_IAM --parameter-overrides BucketNamePrefix=<BUCKET_PREFIX>
```
It will create necessary resources and link them according to the template using cloudformation.

## Test
Upload an image in `JPEG` format to the `SourceBucket` defined in the template and verify the same image with smaller size in `DestBucket`.

72 changes: 72 additions & 0 deletions examples/2016-10-31/image_resize_golang/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"bytes"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/nfnt/resize"
"image"
"image/jpeg"
"log"
"os"
)

func Handler(event events.S3Event) (string, error) {
dstBucket := os.Getenv("DestBucket")
svc := s3.New(session.New())
downloader := s3manager.NewDownloaderWithClient(svc)
uploader := s3manager.NewUploaderWithClient(svc)

for _, record := range event.Records {
srcBucket := record.S3.Bucket.Name
srcKey := record.S3.Object.Key
imgSize := record.S3.Object.Size

file := make([]byte, imgSize)
_, err := downloader.Download(aws.NewWriteAtBuffer(file),
&s3.GetObjectInput{
Bucket: aws.String(srcBucket),
Key: aws.String(srcKey),
})
if err != nil {
log.Fatalln("Donwload error: " + err.Error())
}

reader := bytes.NewReader(file)
img, _, err := image.Decode(reader)
if err != nil {
log.Fatalln("Decode error: " + err.Error())
}

resizedImg := resize.Resize(300, 0, img, resize.Lanczos3)

buf := new(bytes.Buffer)
err = jpeg.Encode(buf, resizedImg, &jpeg.Options{
Quality: 50,
})
if err != nil {
log.Fatalln("Encode error: " + err.Error())
}

imgBody := bytes.NewReader(buf.Bytes())

_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(dstBucket),
Key: aws.String(srcKey),
Body: imgBody,
})
if err != nil {
log.Fatalln("Upload error: " + err.Error())
}
}

return "Resize successful", nil
}

func main() {
lambda.Start(Handler)
}
54 changes: 54 additions & 0 deletions examples/2016-10-31/image_resize_golang/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A function is triggered off an upload to a bucket. It uploads a resized image to another bucket.

Globals:
Function:
Timeout: 20

Parameters:
BucketNamePrefix:
Type: String
Default: sam-example

Resources:
ImageProcessorFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: main
Runtime: go1.x
Tracing: Active
Policies:
- S3ReadPolicy:
BucketName: !Sub "${BucketNamePrefix}-source-bucket"
- S3CrudPolicy:
BucketName: !Sub "${BucketNamePrefix}-dest-bucket"
Environment:
Variables:
DestBucket: !Sub "${BucketNamePrefix}-dest-bucket"
Events:
ImageUpload:
Type: S3
Properties:
Bucket: !Ref SourceBucket
Events: s3:ObjectCreated:*

SourceBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${BucketNamePrefix}-source-bucket"

DestBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${BucketNamePrefix}-dest-bucket"

Outputs:
SourceBucket:
Description: "S3 Bucket name that will trigger a Lambda function upon new objects insertion"
Value: !Ref SourceBucket
DestBucket:
Description: "S3 Bucket name that will store a resized image"
Value: !Ref DestBucket

2 changes: 1 addition & 1 deletion examples/2016-10-31/image_resize_python/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ Outputs:
Description: "S3 Bucket name that will trigger a Lambda function upon new objects insertion"
Value: !Ref SourceBucket
DestBucket:
Description: "S3 Bucket name that will trigger a Lambda function upon new objects insertion"
Description: "S3 Bucket name that will store the resized versions of the images from the source bucket"
Value: !Ref DestBucket
Loading

0 comments on commit 805cc85

Please sign in to comment.