AWS SAM / CloudFormation

Note: This page is auto-generated from the latest SAM template.

SAM Template

Template Description & Transformation Information:

AWSTemplateFormatVersion: “2010-09-09”

Transform: AWS::Serverless-2016-10-31

Description: When a file is saved to S3 Bucket, API is called

Parameters:

EnvType:

Description: Environment type.

Default: dev

Type: String

AllowedValues: [prod, dev, demo]

ConstraintDescription: must specify prod, dev, or demo.

Globals:

Function:
Timeout: 60

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst

Resources:

# DynamoDB Table

# This table is used to keep track of file processing
Table:

Type: AWS::DynamoDB::Table Properties:

TableName: !Sub ‘bucket-to-api-table-${EnvType}’ ProvisionedThroughput:

ReadCapacityUnits: 5 WriteCapacityUnits: 5

KeySchema: - AttributeName: RequestId

KeyType: HASH

AttributeDefinitions: - AttributeName: RequestId

AttributeType: S

# Main Helper Bucket

# This bucket is employed to store files used or created by the application, e.g. the configuration file
WorkflowBucket:

Type: AWS::S3::Bucket Properties:

BucketName: !Sub “bucket-to-api-workflow-bucket-${EnvType}”

# Enabled Parameter

# This parameter will determine if the workflow processes files (which is stored in the AWS Parameter Store), as well as any other future configurations we may add

Type: AWS::SSM::Parameter Properties:

AllowedPattern: String Description: Whether file data should be sent to api by bucket-to-api app Name: !Sub “bucket-to-api-enabled-${EnvType}” Policies: String Type: String Value: False

# Testing Bucket

# This bucket is used in place of the bucket that files will be placed in in production
TestBucket:

Type: AWS::S3::Bucket Properties:

BucketName: !Sub “bucket-to-api-test-bucket-${EnvType}”

# Main Lambda Function

# this Lambda function is the workhorse of the application, and triggers most other components
WorkflowMainFunction:

Type: AWS::Serverless::Function Properties:

FunctionName: !Sub “bucket-to-api-workflow-main-function-${EnvType}” CodeUri: AWS/Services/Lambda/workflow_main_function/ Handler: workflow_main_function.lambda_handler Runtime: python3.8 Environment:

Variables:
ENV_TYPE: !Ref EnvType TABLE_NAME: !Sub ‘bucket-to-api-table-${EnvType}’
Events:
BucketEvent1:

Type: S3 Properties:

Bucket: !Ref TestBucket Events: s3:ObjectCreated:*
Policies:

#- S3ReadPolicy: # BucketName: !Ref TestBucket - S3CrudPolicy:

BucketName: !Ref WorkflowBucket
  • DynamoDBCrudPolicy:
    TableName: !Sub ‘bucket-to-api-table-${EnvType}’
  • SNSPublishMessagePolicy:
    TopicName: !Sub “bucket-to-api-sns-topic-${EnvType}”

# Code Snippets, retained as reference:

# Enable/Disable Function

# this Lambda function is used to enable or disable the processing of files in the application by editing the config file

# ToggleFunction:

# Type: AWS::Serverless::Function

# Properties:

# CodeUri: AWS/Services/Lambda/toggle_function/

# Handler: toggle_function.lambda_handler

# Runtime: python3.8

# Events: # ActivateDeactivate:

# Type: Api

# Properties:

# # Path: /toggle

# Method: ANY

# Policies:

# - S3CrudPolicy:

# BucketName: !Ref WorkflowBucket

# Outputs:

# Enable/Disable API

# this API allows administrators to trigger the lambda function that enables or disables the processing of files by the application
# ToggleApi:

# Description: “Allows you to activate, deactivate, and check the current status of the workflow”

# Value: !Sub “https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${EnvType}/activatedeactivate/”

# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function

# Find out more about other implicit resources you can reference within SAM

# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api

# Sample API and Function

# these are retained for reference

# HelloWorldApi:

# Description: “API Gateway endpoint URL for Prod stage for Hello World function”

# Value: !Sub “https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/”

# HelloWorldFunction:

# Description: “Hello World Lambda Function ARN”

# Value: !GetAtt HelloWorldFunction.Arn

# HelloWorldFunctionIamRole:

# Description: “Implicit IAM Role created for Hello World function”

# Value: !GetAtt HelloWorldFunctionRole.Arn