LoginSignup
0
0

More than 3 years have passed since last update.

CodePiplineでLambda関数デプロイ自動化

Last updated at Posted at 2020-07-17

1.Lambda関数と設定ファイルをCodeCommitへpush

①lambda_function.py

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'body': json.dumps('Hello World!')
    }

②buildspec.yml

version: 0.2

phases:
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
  build:
    commands:
      - export BUCKET=cloudform1
      - aws cloudformation package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
      - echo Nothing to do in the build phase...
artifacts:
  type: zip
  files:
    - template.yml
    - outputtemplate.yml

③template.yml

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
  testDeploy:
    Type: AWS::Serverless::Function
    Properties:
      Description: 'I am Urata'
      FunctionName: 'Dep4'
      Handler: 'lambda_function.lambda_handler'
      Role : 'arn:aws:iam::773181940404:role/lambdafull'
      Runtime: 'python3.6'
      MemorySize: 128
      Timeout: 15
      CodeUri: ./
      Events:
        DepApi:
          Type: Api
          Properties:
            Path: /DepResource
            Method: GET

2.CodePipelineを以下の手順でセット

パイプラインを作成するには
「開発者向けツールコンソール」を開きます。
[パイプライン] で、[パイプライン] を選択します。
[Create pipeline] を選択します。
パイプライン設定を行い、[次へ] を選択します。
パイプライン名 – lambda-pipeline
サービスロール – 新しいサービスロール
アーティファクトストア – デフォルトの場所
ソースステージ設定を行い、[次へ] を選択します。
ソースプロバイダー – AWS CodeCommit
リポジトリ名 – lambda-pipeline-repo
ブランチ名 – master
変更検出オプション – Amazon CloudWatch Events
[ビルドプロバイダー] で、[AWS CodeBuild]、[プロジェクトの作成] の順に選択します。
ビルドプロジェクト設定を行い、[CodePipeline に進む] を選択します。
プロジェクト名 – lambda-pipeline-build
オペレーティングシステム – Ubuntu
ランタイム – 標準
ランタイムバージョン – aws/codebuild/standard:2.0
イメージのバージョン – 最新
Buildspec 名 – buildspec.yml
[次へ] を選択します。
デプロイステージ設定を行い、[次へ] を選択します。
デプロイプロバイダー – AWS CloudFormation
アクションモード – 変更セットの作成または置換
スタック名 – lambda-pipeline-stack
変更セット名 – lambda-pipeline-changeset
テンプレート – BuildArtifact::outputtemplate.yml
機能 – CAPABILITY_IAM、CAPABILITY_AUTO_EXPAND
ロール名 – cfn-lambda-pipeline
[Create pipeline] を選択します。
IAM コンソールの [Roles] ページを開きます。
[codebuild-lamba-pipeline-build-service-role] を選択します。
[Attach policies (ポリシーをアタッチします)] を選択します。
[AmazonS3FullAccess] をアタッチします。
デプロイステージを更新するには
「開発者向けツールコンソール」でパイプラインを開きます。
[Edit] を選択します。
[デプロイ] の横にある [ステージを編集] を選択します。
[アクショングループの追加] を選択します。
デプロイステージ設定を行い、[次へ] を選択します。
アクション名 – execute-changeset
アクションプロバイダ – AWS CloudFormation
入力アーティファクト – BuildArtifact
アクションモード – 変更セットの実行
スタック名 – lambda-pipeline-stack
変更セット名 – lambda-pipeline-changeset

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0