Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use output artifact of CodeBuild in CloudFormation?

So I have a fairly simple stack I'm trying to setup consisting of a single Lambda function subscribed to an SNS topic. I'd like to use CodePipeline with three stages: Source (GitHub) -> Build (CodeBuild) -> Deploy (CloudFormation).

I managed to cobble together a template and buildspec file that that work, except I'm lost on how I'm supposed to reference the output artifact that CodeBuild makes in the CloudFormation template; right now I just have placeholder inline code.

Basically, what am I supposed to put in the Code: property of the Lambda function in order to get the CodeBuild files (which is my output artifact in CodePipeline)?

template.yml:

AWSTemplateFormatVersion: 2010-09-09
Resources:
  SNSTopic:
    Type: 'AWS::SNS::Topic'
    Properties:
      Subscription:
        - Endpoint: !GetAtt
            - LambdaFunction
            - Arn
          Protocol: lambda
  LambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Runtime: python3.6
      Handler: main.lamda_handler
      Timeout: '10'
      Role: !GetAtt
        - LambdaExecutionRole
        - Arn
      Code:
        ZipFile: >
          def lambda_handler(event, context):
            print(event)
            return 'Hello, world!'
  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
  LambdaInvokePermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      FunctionName: !GetAtt
        - LambdaFunction
        - Arn
      Action: 'lambda:InvokeFunction'
      Principal: sns.amazonaws.com
      SourceArn: !Ref SNSTopic

buildspec.yml:

version: 0.2
phases:
  install:
    commands:
      - pip install -r requirements.txt -t libs
artifacts:
  type: zip
  files:
    - template.yml
    - main.py
    - lib/*
like image 306
mth Avatar asked Jul 03 '17 22:07

mth


People also ask

How do I access CodeBuild output?

To get the build output artifact (Amazon S3 console)Open codebuild- region-ID - account-ID -output-bucket . Open the codebuild-demo-project folder. Open the target folder, where you find the messageUtil-1.0. jar build output artifact file.

What is a CodeBuild artifact?

Artifacts is a property of the AWS::CodeBuild::Project resource that specifies output settings for artifacts generated by an AWS CodeBuild build.

Where are CodeBuild artifacts stored?

zip , the output artifact is stored in MyArtifacts/<build-ID>/MyArtifact. zip . If this flag is set, a name specified in the buildspec file overrides the artifact name. The name specified in a buildspec file is calculated at build time and uses the Shell Command Language.

Where do the build artifacts are stored after AWS CodeBuild service completes CodeBuild?

All artifacts that are specified as input artifacts to a CodeBuild action are available inside of the container running the commands. CodeBuild can provide either a build or test action. For more information, see the AWS CodeBuild User Guide.


1 Answers

I realize this question is old, but thought I'd answer it with respect to SAM

project_root/
  template.yaml
  buildspec.yaml
  my_lambda/
    my_lambda.py
    requirements.txt

template.yaml:

Transform: AWS::Serverless-2016-10-31

Resources:
  MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: my_lambda.lambda_handler
      CodeUri: my_lambda/
      Runtime: python3.8

buildspec.yaml:

version: 0.2

phases:
  install:
    runtime-versions:
      python: 3.8
    commands:
      - pip install aws-sam-cli
  build:
    commands:
      - sam build
      - sam package --s3-bucket mybucket --s3-prefix sam | sam deploy -t /dev/stdin --stack-name FOOSTACK --capabilities CAPABILITY_IAM

Notes:

  1. sam build will pip install your lambda requirements.txt
  2. sam package will zip your lambda, and name it with the md5 of its contents and upload to S3 for you (only if it has changed)
  3. sam deploy will create a CloudFormation changeset and run it for you
like image 100
Neil McGuigan Avatar answered Sep 17 '22 21:09

Neil McGuigan