DEV Community

KenjiGoh
KenjiGoh

Posted on

2 ways to create Lambda with Cloudformation

1. Inline Code

Cloudformation:

AWSTemplateFormatVersion: "2010-09-09"
Description: Lambda inline code
Resources:
  MyInlineLambda:
    Type: AWS::Lambda::Function
    Properties: 
      Runtime: python3.x
      Role: arn:aws:iam::123456789012:role/lambda-role
      Handler: index.handler
      Code:
        ZipFile: |
          import os

          DB_URL = os.getenv("DB_URL")
          db_client = db.connect(DB_URL)
          def handler(event, context):
            user = db_client.get(user_id = event["user_id"])
            return user
Enter fullscreen mode Exit fullscreen mode

2. Through Zip File on S3

Cloudformation:

AWSTemplateFormatVersion: "2010-09-09"
Description: Lambda from S3
Resources:
  MyLambdaFromS3:
    Type: AWS::Lambda::Function
    Properties: 
      Runtime: nodejs18.x
      Role: arn:aws:iam::123456789012:role/lambda-role
      Handler: index.handler
      Code:
        S3Bucket: some-bucket
        S3Key: function.zip
        S3ObjectVersion: String
Enter fullscreen mode Exit fullscreen mode

Serverless Framework:

service: my-service

package:
  individually: true

functions:
  hello:
    handler: com.serverless.Handler
    package:
      artifact: s3://some-bucket/path/to/function.zip
Enter fullscreen mode Exit fullscreen mode

Top comments (0)