AWS日記39 (AWS CodePipeline)

# はじめに
今回は [AWS CodePipeline](https://aws.amazon.com/jp/codepipeline/) を試します。
[CodeCommit](https://aws.amazon.com/jp/codecommit/)のリポジトリが変更された際に、[CodeDeploy](https://aws.amazon.com/jp/codedeploy/)を利用してEC2にデプロイします。
[チュートリアル: シンプルなパイプラインを作成する (CodeCommit リポジトリ)](https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-simple-codecommit.html)を参考にします。


# CodeCommit リポジトリを作成

 - [ステップ 1](https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-simple-codecommit.html#codecommit-create-repository)

![01.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/5fa281f9-00a3-868e-f317-cc5d17b4443e.jpeg)

![02.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/06cab243-2679-90b9-f673-1117129bdac7.jpeg)


# EC2 Linux インスタンスを作成して CodeDeploy エージェントをインストール

 - [ステップ 3](https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-simple-codecommit.html#codecommit-create-deployment)

![10.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/48b0c813-7ed8-2994-be2d-72e82537c5da.jpeg)

![11.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/612a52e2-23b2-9fc5-720a-89b39abd5cbe.jpeg)

![15.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/1eda211f-0028-eaeb-01fd-a1b6e2ad02a8.jpeg)

![16.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/59af6e19-daa5-07ac-16d4-d4e08bdb732f.jpeg)

![12.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/d9830f01-c950-c2d0-0d55-833ed053a864.jpeg)

![13.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/4a4cfadd-b1a4-bbc1-9d18-1b7e577e4a2b.jpeg)

![17.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/2428eb58-4029-c75e-fc38-b96b928d5dc2.jpeg)

![18.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/91fd7929-2d52-7667-7c7f-8e3cbdfd42c2.jpeg)


# CodeDeploy でアプリケーションを作成

 - [ステップ 4](https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-simple-codecommit.html#codecommit-create-codedeploy-app)

![20.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/ebd6dd3a-a88d-8cbd-7f99-00394bd2d8e1.jpeg)

![21.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/940b1dab-3f9e-20b0-b93e-274efd2482ba.jpeg)

![22.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/8a228c25-5bfd-3ba2-fff2-3c0720dc0bf7.jpeg)

![23.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/a96bafc6-75ea-a250-4ddb-bc395aab5046.jpeg)

![24.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/873596a4-1f50-b4ab-2b14-357699de932c.jpeg)

![25.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/1f920c28-d2ff-3fa7-572d-4ebc625c789b.jpeg)

![26.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/dd761a5c-0650-11da-693f-d9bf404957f0.jpeg)

![27.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/43c0c6a3-3bb4-6288-14d0-b303dcbbaaa7.jpeg)


# CodePipeline で最初のパイプラインを作成

 - [ステップ 5](https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-simple-codecommit.html#codecommit-create-pipeline)

![28.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/78fe4c5d-cbe1-fda0-8cd3-736fdd9fe552.jpeg)

![29.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/780ed51c-3128-394c-d272-254af0032102.jpeg)

![30.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/c01580d8-d2e7-658b-d90e-a111823c1825.jpeg)

![31.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/563f4acb-4c6c-6c11-8904-28acfb499080.jpeg)

![32.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/c5003eb4-8bd7-bf96-c65a-f16625671e23.jpeg)

![33.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/531cc8b5-9220-256f-6513-061f8a621e57.jpeg)

![34.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/dd447f7e-9ed8-76fa-c996-9e9aac2b5f5d.jpeg)


### パイプライン作成後、自動的に実行される

![35.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/0e383c0c-d805-5a33-13ab-e7cdea9db660.jpeg)


# 確認

### 初回

![36.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/ccea0a63-7fa6-4a35-df10-f0e9a6c77348.jpeg)


 - [ステップ 6: CodeCommit リポジトリ内のコードを変更する](https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-simple-codecommit.html#codecommit-push-code)

### 変更後

![37.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/181d3dde-396c-f5fb-bf73-878e3ae149c3.jpeg)


# 削除

 - [ステップ 7: リソースをクリーンアップする](https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-simple-codecommit.html#codecommit-clean-up)

### CodePipeline削除
![38.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/fe637d10-69ef-8f6c-d470-2f76b69500fc.jpeg)


### CodeDeployアプリケーション削除
![39.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/5aeec38e-540c-66a3-5e5f-e5b353eaa71f.jpeg)


### EC2インスタンス停止
![50.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/62c0ff87-3558-8b5a-1f3d-70f56048d044.jpeg)


### EC2インスタンス終了
![51.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/b4516aa4-e5fc-02b0-67a6-0136f77e4e30.jpeg)



### CodeCommitリポジトリ削除
![52.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/586707/c82ce13d-4c1f-19e2-7927-e7fe92384e99.jpeg)



# 終わりに
AWS CodePipeline を試しました。
今回はビルドステージをスキップしたパイプラインを作成しましたが、今後デプロイ前にコードをビルドする手順を含むパイプラインを試そうと思います。

##### 料金・無料利用枠
[AWS CodePipeline の料金](https://aws.amazon.com/jp/codepipeline/pricing/)

##### 以下のイベントが記事作成の動機になりました
[AWS DevAx::connect シーズン 3 「rethink CI/CD」](https://pages.awscloud.com/JP2022_DevAxconnect_Season3.html?trk=aws_event_page_devaxconnect_s3)