LoginSignup
2
0

More than 3 years have passed since last update.

circleCI でaws cliを使ってlamda環境にデプロイする

Last updated at Posted at 2020-02-16

circleCi2.0にaws-cliのorbsが追加されたようなので使ってみました。
https://circleci.com/orbs/registry/orb/circleci/aws-cli

この記事でやること

vue.jsで作成したプロジェクトをGitにpushしたときに、CircleCiによりビルド、デプロイされるようにします。
cloudFormationのcliを利用してデプロイしたいので、awsコマンドが使えるようにしたいと思います。

CircleCIの設定

環境変数の追加

環境変数を設定します。
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_DEFAULT_REGION

加えて、今回cloudFormationでLamdaにアップロードするビルド済コードの置き場所にS3バケットを指定しているのと、スタック名の指定をしているので環境変数に加えておきます。ベタ書きでもいいと思います。
- S3_BUCKET_NAME
- STACK_NAME
Screen-Shot-2020-02-16-at-17.24.36.jpg

使うIAMの切り替えをしたい場合は環境変数を追加しておいて、引数として与えれば切り替えができそうです。

config.ymlの記述

完成したyamlです。

version: 2.1
orbs:
  aws-cli: circleci/aws-cli@0.1.20
executors:
  default:
    working_directory: ~/workspace
    docker:
      - image: circleci/node:12.10
commands:
  prepare_node_dependency:
    description: "install node.js"
    steps:
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install Dependencies
          command: npm ci
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: Check if Nodejs is Installed
          command: |
            node --version
            npm --version
  package_and_deploy:
    description: "build and deploy project"
    steps:
      - run:
          name: build project
          command: |
            npm run build
      - run:
          name: create package
          command: |
            aws cloudformation package --template-file cloudformation.yaml --s3-bucket $S3_BUCKET_NAME --output-template-file cloudformation_dist.yaml
      - run:
          name: deploy package
          command: |
            aws cloudformation deploy --template-file cloudformation_dist.yaml --stack-name $STACK_NAME --capabilities CAPABILITY_IAM
jobs:
  install_and_setup_cli:
    executor:
      name: default
    steps:
      - aws-cli/install
      - aws-cli/setup
  master_build_and_test:
    executor:
      name: default
    steps:
      - checkout
      - aws-cli/install
      - aws-cli/setup
      - prepare_node_dependency
      - package_and_deploy

workflows:
  master-build:
    jobs:
      - master_build_and_test:
          filters:
            branches:
              only: master

解説

orbsにaws-cliを指定します。対応バージョンは2.1のようです。

version: 2.1
orbs:
  aws-cli: circleci/aws-cli@0.1.20

jobsで aws-cli/install とaws-cli/setup を指定します。

jobs:
  install_and_setup_cli:
    executor:
      name: default
    steps:
      - aws-cli/install
      - aws-cli/setup

デフォルトだと環境変数に指定したAWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY、AWS_DEFAULT_REGIONからプロファイルを作るようですが、引数として別の値を指定することもできるようです。

    steps:
      - aws-cli/install
      - aws-cli/setup:
            aws-access-key-id: 'OthereAwsAccessKeyId'
            aws-secret-access-key: 'OthereAwsAccessKeyId'

利用するAWS cliをcommandsの中に書き込んでおきます。

commands:
#中略
  package_and_deploy:
    description: "build and deploy project"
    steps:
      - run:
          name: build project
          command: |
            npm run build
      - run:
          name: create package
          command: |
            aws cloudformation package --template-file cloudformation.yaml --s3-bucket $S3_BUCKET_NAME --output-template-file cloudformation_dist.yaml
      - run:
          name: deploy package
          command: |
            aws cloudformation deploy --template-file cloudformation_dist.yaml --stack-name $STACK_NAME --capabilities CAPABILITY_IAM

steps の中で、AWS cliのインストールと設定が終わったあとに実行します。

    steps:
      - checkout
      - aws-cli/install
      - aws-cli/setup
      - prepare_node_dependency
      - package_and_deploy
2
0
1

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
2
0