Jens Madsen
Jens Madsen

Reputation: 1610

How to automatically deploy api gateway in AWS codepipeline

Currently I am able to deploy a lambda by pushing to github. I also automatically deploy a lambda but only because the api gateway is an event in the lambda yaml file

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Identifies paragraphs in documents and links to the law
Resources:
  LambdaParagraphLinker:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: LambdaParagraphLinker.lambda_handler
      Runtime: python3.6
      CodeUri: ./
      Description: Identifies paragraphs in documents and links to the 
      law
       MemorySize: 512
      Timeout: 10      
      Events:
        Api:
          Type: Api
          Properties:
            Path: /LambdaParagraphLinker
            Method: ANY

How can I deploy an api gateway using a swagger file ?

Upvotes: 0

Views: 3516

Answers (2)

Bruce Allen
Bruce Allen

Reputation: 91

Hands down the best way to do this in codepipeline is by using https://serverless.com/ framework. This replaces every super complicated hack-job and workaround I've previously used. Way less complicated IMO.

Create a codepipeline, link it to src & a codebuild project, set a few permissions, done.

//serverless.yml

service: my-api
provider:
  name: aws
  runtime: python2.7

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: api/v1/message
          method: post

//buildspec.yml

version: 0.2
phases:
  install:
    commands:
      #BUILD
      - sudo apt-get update -y
  build:
    commands:
      - echo $environment
      - serverless package --stage $environment --region us-east-1
      - serverless deploy --stage $environment --region us-east-1

Or torture yourself by doing one of the options below...

You can do this in cloudformation from within code pipeline. Export the swagger spec from within the gatewayapi console and place in the cloudformation template.

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  PlayersAPI:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: MyApi
      Description: API Description
      Body:
       "SWAGGER HERE"

Hooking this up to lambda is a little bit cumbersome but I can describe the steps. First create a codepipeline project with source, build, and deploy steps.

  • src should be standard from github or codecommit
  • build should output a zip file and use buildspec.yml Something like this...

//buildspec.yml

version: 0.1
phases:
  install:
    commands:
      #BUILD
      - zip -r lambda.zip . -x *.git*
artifacts:
  files:
    - '**/*.zip'
    - '**/*.yml'
  discard-paths: no

Have the build step export an artifact MyAppBuild (or whatever you want to call it)

The final pipeline step is creating the lambda function in this repo as a standalone function through the console(its reusable): https://github.com/tkntobfrk/codepipeline-lambda-s3

This lambda function downloads the pipeline artifact/zipped lambda function and updates it using boto.

After these steps you could add another step as a cloudformation deploy step. Connect it to the lambda function that you've just deployed.

If you are dealing with multiple environments you could create lambda functions and gatewayapi cloudformation template for each environment then run them in sequence.

  • stage 1: src
  • stage 2: build
  • stage 3: deploy lambda test, deploy gateway api cloudformation test
  • stage 4: validate test
  • stage 5: deploy lambda prod, deploy gateway api cloudformation prod

Using straight AWS serverless like this works too. However, you need to use a standard artifact location for the uri's. The DefinitionUri: for the API can be the exported swagger from the gatewayapi console.

//cloudformation.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  MySimpleFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.lambda_handler
      Runtime: python2.7
      CodeUri: s3://somebucket/somezip.zip
  MyAPI:
     Type: AWS::Serverless::Api
     Properties:
        StageName: prod
        DefinitionUri: s3://somebucket/somezip.zip

Upvotes: 2

jackko
jackko

Reputation: 7344

AWS::Serverless::Api

https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessapi

You can find Swagger docs all over the place, and docs on API Gateway extensions are in the developer guide. I would start by going into the API Gateway console and look at the API that Lambda creates for you. You can go to the 'Stages' page and for any stage, you can Export the API as Swagger.

Upvotes: 0

Related Questions