Hamed Minaee
Hamed Minaee

Reputation: 2560

How to use lambda to deploy latest code put in s3 to lambda function

I am trying to deploy my lambda function anytime I s3 bucket containing it updates.

If I know I have the the latest zip lambda code in a bucket I can simply use cloud formation to automate the creation and deployment of lambda function

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation CloudWatch Log Janitor Demo Stack",
"Resources": {
    "TestLamdaRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [{
                    "Effect": "Allow",
                    "Principal": {
                        "Service": [
                            "lambda.amazonaws.com"
                        ]
                    },
                    "Action": [
                        "sts:AssumeRole"
                    ]
                }]
            },
            "Path": "/"
        }
    },
    "EbsBackupExecutionPolicy": {
        "DependsOn": [
            "TestLamdaRole"
        ],
        "Type": "AWS::IAM::Policy",
        "Properties": {
            "PolicyName": "hamedlamdapolicytest",
            "Roles": [{
                "Ref": "TestLamdaRole"
            }],
            "PolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [{
                        "Effect": "Allow",
                        "Action": [
                            "logs:*"
                        ],
                        "Resource": [
                            "arn:aws:lambda:us-east-1:1111111111111:function:*"
                        ]
                    },
                    {
                        "Effect": "Allow",
                        "Action": [
                            "ec2:Describe*"
                        ],
                        "Resource": [
                            "*"
                        ]
                    }
                ]
            }
        }
    },
    "LambdaFuction": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "S3Bucket": "lambda-dep-test",
                "S3Key": "index.zip"
            },
            "Role": {
                "Fn::GetAtt": [
                    "TestLamdaRole",
                    "Arn"
                ]
            },
            "Timeout": 60,
            "Handler": "lambda_function.handler",
            "Runtime": "nodejs6.10",
            "MemorySize": 128,
            "FunctionName": "stg1-test"
        }
    }
}

}

But the problem is that as soon as I run the above lambda code then whenever user put sth in the bucket the latest code does not get deployed automatically. I Know it has sth to do with lambda but I am lost and I do not know which approach to use and where to start. Can you please shed light on this?

Upvotes: 0

Views: 996

Answers (2)

Laurent Jalbert Simard
Laurent Jalbert Simard

Reputation: 6339

This is how I tackled this issue:

  • Enable versioning in the lambda-dep-test bucket
  • In your AWS::Lambda::Function declaration in your CloudFormation template, use the S3ObjectVersion property in the Code section to specify which version should be deployed.

Now you can either update the template and specify a new S3ObjectVersion every time the lambda code is updated in the bucket or you can declare it as a parameter in your template and reference it in S3ObjectVersion. Both solutions can then be scripted with the packaging and uploading of your .zip file.

Upvotes: 1

Suken Shah
Suken Shah

Reputation: 1672

Alternatively use frameworks like serverless. They make deployment easier using simple commands and easy to integrate with your CI. https://serverless.com

Upvotes: 0

Related Questions