1
votes

I'm trying to deploy a lambda function to AWS using CloudFormation. My problem is that for the code section I want to give a local file rather than a s3 bucket. This is what I have at the moment.

  getStores:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.get
      Role: !GetAtt SyncGalaxyLambdaRole.Arn
      Code:
        ZipFile:
          Fn::Join:
          - "\n"
          - - exports.get = function(event, context) {
            - "  console.log('hello world!');"
            - "};"
      Runtime: nodejs6.10

Now for the Code section I want to give the contents of a local file generated through webpack. How do I do this?

1

1 Answers

1
votes

You may avoid giving the Javascript code inline with the cloudformation. Which will be hard to maintain in future. You can also use AWS SAM to zip the code to s3 bucket and use for lambda function. Which is the recommended method from AWS. http://docs.aws.amazon.com/lambda/latest/dg/deploying-lambda-apps.html

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Hello World
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs4.3
      CodeUri: ./

Perform the following steps to create the stack.

  1. Install and configure AWS CLI.
  2. aws s3 mb s3://[STACK_NAME]-app-artifact --region [REGION]
  3. Run your webpack step to generate the code.
  4. aws cloudformation package --template-file samTemplate.yaml --s3-bucket [STACK_NAME]-app-artifact --output-template-file NewSamTemplate.yaml
  5. aws --region [REGION] cloudformation deploy --template-file NewSamTemplate.yaml --stack-name [STACK_NAME] --capabilities CAPABILITY_IAM.

When your code is updated you can just re run the above steps to create the stack.