# This is the SAM template that represents the architecture of your serverless application # https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html # https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md # The AWSTemplateFormatVersion identifies the capabilities of the template # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html AWSTemplateFormatVersion: 2010-09-09 Description: >- sam-app # Transform section specifies one or more macros that AWS CloudFormation uses to process your template # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html Transform: - AWS::Serverless-2016-10-31 Parameters: ProjectName: Type: String Stage: Type: String Conditions: CreateProdResources: !Equals [!Ref Stage, Prod] # Read more about Global variables here!! # https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#globals-section Globals: Function: Tracing: Active MemorySize: 1024 Timeout: 60 Runtime: nodejs14.x # Read more about Deployment Preferences and Safe Deployments here!! # https://github.com/awslabs/serverless-application-model/blob/master/docs/safe_lambda_deployments.rst AutoPublishAlias: live DeploymentPreference: Enabled: true Type: !If [CreateProdResources, Canary10Percent5Minutes, AllAtOnce] Environment: Variables: # Enable usage of KeepAlive to reduce overhead of short-lived actions, like DynamoDB queries AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1 Api: OpenApiVersion: 3.0.1 TracingEnabled: true Cors: AllowMethods: "'OPTIONS,POST,GET'" AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'" AllowOrigin: "'*'" # Resources declares the AWS resources that you want to include in the stack # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html Resources: Api: Type: AWS::Serverless::Api Properties: Name: !Sub ${ProjectName}-${Stage} StageName: !Sub ${Stage} # AccessLogSetting: # - DestinationArn: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group/custom_log_group_name" # Format: "$context.identity.sourceIp $context.identity.caller $context.identity.user [$context.requestTime] "$context.httpMethod $context.resourcePath $context.protocol" $context.status $context.responseLength $context.requestId" MethodSettings: - MetricsEnabled: True ResourcePath: '/*' HttpMethod: '*' #ThrottlingRateLimit: 100 #ThrottlingBurstLimit: 50 # Each Lambda function is defined by properties: # https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction # This is a Lambda function config associated with the source code: get-all-items.js getAllItemsFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./ # Defined where package.json file must reside for "sam build" to complete Handler: src/items/get-all-items/index.getAllItemsHandler Description: A simple example includes a HTTP get method to get all items from a DynamoDB table. Policies: # Give Create/Read/Update/Delete Permissions to the SampleTable - DynamoDBCrudPolicy: TableName: !Ref SampleTable Environment: Variables: # Make table name accessible as environment variable from function code during execution SAMPLE_TABLE: !Ref SampleTable Events: Api: Type: Api Properties: RestApiId: !Ref Api Path: /items Method: GET getByIdFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./ Handler: src/items/get-by-id/index.getByIdHandler Description: A simple example includes a HTTP get method to get one item by id from a DynamoDB table. Policies: - DynamoDBCrudPolicy: TableName: !Ref SampleTable Environment: Variables: SAMPLE_TABLE: !Ref SampleTable Events: Api: Type: Api Properties: RestApiId: !Ref Api Path: /items/{id} Method: GET putItemFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./ Handler: src/items/put-item/index.putItemHandler Description: A simple example includes a HTTP post method to add one item to a DynamoDB table. Policies: - DynamoDBCrudPolicy: TableName: !Ref SampleTable Environment: Variables: SAMPLE_TABLE: !Ref SampleTable Events: Api: Type: Api Properties: RestApiId: !Ref Api Path: /items Method: POST SampleTable: Type: AWS::DynamoDB::Table Properties: BillingMode: PAY_PER_REQUEST AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH TableName: !Sub sampleTable-${ProjectName}-${Stage} Outputs: ApiUrl: Description: "API Gateway endpoint URL for ${Stage} stage" Value: !Sub "https://${Api}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/" SampleTable: Value: !GetAtt SampleTable.Arn Description: Sample Data Table ARN