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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay