Friday, December 9, 2022

Sample Deployment Template for AWS Lambda with S3 access:

 Problem Statement: Deploy a serverless application for uploading files to S3 storage. Additionally, create a template to help with CI/CD of such an application.

Solution: One of the ways this problem can be solved is by leveraging AWS cloud resources such as AWS Lambda, AWS API gateway, and S3 storage. The CloudFormationTemplate for this would appear as shown below. The bucket name would be a parameter. The lambda is charged in the increments of 100ms usage. It’s size is determined by its memory. The Lambda Integrations are indicated by the keyword proxy. The code is uploaded to a bucket indicated by the codeUri. The invocation handler is also indicated by the properties.

AWSTemplateFormatVersion: '2010-09-09'

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

Description: Serverless web application for uploading files to S3

Globals:

  Api:

    BinaryMediaTypes:

    - '*~1*'

Resources:

  uploader:

    Type: AWS::Serverless::Function

    Properties:

      Description: Serverless web application for uploading files to S3

      Handler: src/index.handler

      Runtime: nodejs12.x

      CodeUri:

        Bucket: awsserverlessrepo-changesets-1f9ifp952i9h0

        Key: 536706842180/arn:aws:serverlessrepo:us-east-1:233054207705:applications-uploader-versions-1.1.0/5176d06e-2d79-4e66-8f0c-a3bccf9084e5

      MemorySize: 1536

      Policies:

      - S3CrudPolicy:

          BucketName:

            Ref: destBucket

      Timeout: 60

      Events:

        root:

          Type: Api

          Properties:

            Path: /

            Method: get

        getProxy:

          Type: Api

          Properties:

            Path: /{proxy+}

            Method: get

        postProxy:

          Type: Api

          Properties:

            Path: /{proxy+}

            Method: post

      Environment:

        Variables:

          DEST_BUCKET:

            Ref: destBucket

Parameters:

  destBucket:

    Type: String

    Description: Name of the S3 Bucket to put uploaded files into (must exist prior to deployment)

 

 

No comments:

Post a Comment