0

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?

1
  • 1
    There is no straightforward way to achieve this. Create an event in S3. Whenever a new file is added, the event will trigger a different Lambda function which will update the original function (in this case: stg1-test) Commented Oct 17, 2017 at 19:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, that would be great if you can give an example for declaring S3ObjectVersion as parameter to temlate in codepipeline, I have seen the aws doc regarding "Fn::GetArtifactAtt" but couldn't find out how I can use it for this case. Can you help with that?
0

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

2 Comments

Thanks good to know about that framework. but what I want is automatic deployment without any user intraction. Does the framework do that ?
Serverless deploys the zip file to configured s3 bucket and triggers the lambda deployment. In your case you would still require user interaction to upload zip file to s3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.