techtsubame’s blog

備忘録であり、何が起きても責任は取りません

CodePipeline to ec2

[:contentes]

概要

Codecommit,CodeBuild,CodeDeployと連携するCodePipeLineを作成し動作検証をする

参考

pages.awscloud.com

CodePipeline

  • Codecommit,CodeBuild,CodeDeployと連携するCodePipeLineを作成

パイプライン

作成

手元PC

ファイル

変更

  • index.htmlファイルを変更
  • git にてadd,commit,push

CodePipeline

パイプライン

確認

  • 正常終了を確認

手元PC

ブラウザ

アクセス

  • 修正したindex.htmlであることを確認

CodeDeploy

概要

  • リソース(アプリケーション)を作成するためのappspec.ymlを作成

    参考

    pages.awscloud.com

docs.aws.amazon.com

IAM

ロール

作成

CodeDeploy

アプリケーション

作成

* 名前を控える

手元PC

ビルドファイル

変更

  • xxxの置換後の文字列に控えた名前を設定する
$ sed  -e 's/ xxx / 控えた名前 /g' buildspec.yml
$ 

CodeDeploy

アプリケーション

デプロイグループ

作成

  • サービルロールは作成したロールを指定

  • EC2のインスタンス名を指定する
  • 一覧に表示されない場合はIAMロールの権限またはEC2へのIAMロール割り当てを確認する

  • 複数台に対し徐々に設定する場合には変更すること

手元PC

アプリケーションファイル

作成

cat << "EOF" > ./src/appspec.yml
version: 0.0
os: linux
files:
  - source: index.html
    destination: /var/www/html
EOF
$ 

gitにてpushまで実施

  • git add
  • git commit
  • git push

手元PC

確認

EC2インスタンスにて起動しているwebページにアクセス

CodeBuild

ビルド

実行

デプロイ

実行

手元PC

確認

EC2インスタンスにて起動しているwebページにアクセス

  • 表示されている文字列が変わったことを確認する

CodeBuild

概要

S3

  • アーチファクト(成果物)を格納するバケットを作成

バケット

作成

CodeBuild

ビルドプロジェクト

作成

IAM

  • CodeBuildのビルドプロジェクトで作成されたサービスロールCodeDeployのポリシーをアタッチ

ロール

ポリシーをアタッチ

* AWSCodeDeployDeployerAccess

手元PC

  • ビルドファイルの作成とindex.htmlの移動

ファイル

index.htmlの移動

$ install -d ./src
$ mv index.html ./src/ 
$ 

ビルドファイルの作成

  • 書き方はリファレンスを参照
  • xxxは別途設定する
パラメータ 意味
application-name TBD wordpress_app
s3-location s3のアーチファクトの場所を指定 s3://s3バケット名/リビジョン名.zip
source アップロードされる開発マシン上の場所 src
$ touch buildspec.yml
$ cat << "EOF" > buildspec.yml   
version: 0.2
phases:
  build:
    commands:
      - aws deploy push --application-name xxx --s3-location s3://cicd-artifact/artifact.zip --source src
artifacts:
  files:
    - '**/*'
  base-directory: src
EOF

CodePipeLine to S3

概要

  • CodePipelineを設定し、CodeCommitに変更を加えるとS3と連携しS3上のファイルを変更

参考

pages.awscloud.com

CodePipeLine

作成

1つめの設定

  • ビルドステージはスキップ

S3

アクセス

手元PC

変更

index.html

<!DOCTYPE html>

<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>S3 Static Web Hosting Code Commit CodePipeLine</title>
</head>
<body>
  Hello, AWS World!! CodeCommit S3 CodePipeLine
</body>
</html>
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/index.html b/index.html
index 7986b4d..86b19c9 100644
--- a/index.html
+++ b/index.html
@@ -3,10 +3,9 @@
 <html lang="ja">
 <head>
   <meta charset="utf-8">
-  <title>S3 Static Web Hosting Code Commit</title>
+  <title>S3 Static Web Hosting Code Commit CodePipeLine</title>
 </head>
 <body>
-  Hello, AWS World!! CodeCommit
+  Hello, AWS World!! CodeCommit S3 CodePipeLine
 </body>
$ git add index
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   index.html
$ git commit -m "Mod: Add codepipelone wording"  
[master efc3aa8] Mod: Add codepipelone wording
 1 file changed, 2 insertions(+), 3 deletions(-)
$ git log --oneline  
efc3aa8 (HEAD -> master) Mod: Add codepipelone wording
1382fb6 (origin/master) Add: index.html
$
$ git push origin master

S3

アクセス

* 変更されたindex.htmlが表示されることを確認

CodePipeLine

確認

パイプライン

CodeCommit

参考

docs.aws.amazon.com docs.aws.amazon.com docs.aws.amazon.com

手元のPC

作成

  • 公開鍵ファイル名は必要に応じて変更すること
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_aws_codecommit              
$ cat ~/.ssh/id_rsa_aws_codecommit.pub

CodeCommit

リポジトリ

作成

IAM

ユーザ

アクセス許可

ポリシーアタッチ

鍵設定

手元のPC

git

インストール

  • 省略

ユーザ、MailAddress設定

  • 任意のユーザとMailAddressに変更すること
git-config --global user.name "admin"
git-config --global user.email "admin@local"

確認

git config --list

作成

  • 公開鍵ファイル名は必要に応じて変更すること
$ ssh-keygen
$ cat ~/.ssh/id_rsa.pub

ssh設定ファイルに記載

  • UserはIAMにて設定したAWS CodeCommit の SSH 公開キーに表示されているSSHキーIDを設定すること
  • IdentityFile(秘密鍵ファイル名)は必要に応じて変更すること
$ echo 'Host git-codecommit.*.amazonaws.com
  User IAM SSHキーID
  IdentityFile ~/.ssh/id_rsa_aws_codecommit
' | tee -a ~/.ssh/config

確認

* URLはcodecommitのリポジトリの接続ステップに表示されているものをペーストする

$ git clone ssh://git-codecommit~

ファイル追加

作成

echo '<!DOCTYPE html>

<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>S3 Static Web Hosting Code Commit</title>
</head>
<body>
  Hello, AWS World!! CodeCommit
</body>
</html>
' | tee index.html

ステージング

git status
git add index.html
git status

コミット

git status
git commit -m "コメントを記載"
gi status

CodeCommitへアップロード

git push origin master

AWS

CodeCommit

ファイル確認

CLIインストール for mac

参考

docs.aws.amazon.com

手元のPC

CLI

インストールディレクトリ設定ファイル作成

echo "<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <array>
    <dict>
      <key>choiceAttribute</key>
      <string>customLocation</string>
      <key>attributeSetting</key>
      <string>/Users/`id -un`</string>
      <key>choiceIdentifier</key>
      <string>default</string>
    </dict>
  </array>
</plist>
" | tee ~/choices.xml

pkgのダウンロード

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"

pkgのインストール

installer -pkg AWSCLIV2.pkg \
            -target CurrentUserHomeDirectory \
            -applyChoiceChangesXML choices.xml

シンボリックリンクの作成

INSTALL_dir=$(cat ~/choices.xml | grep  "string>/" | awk -F'[<>]' '{print $3}')
sudo ln -s ${INSTALL_dir}/aws-cli/aws /usr/local/bin/aws
sudo ln -s ${INSTALL_dir}/aws-cli/aws_completer /usr/local/bin/aws_completer

コマンドパス確認

  • 必要に応じて環境変数を再読み込み (source ~/.zshrc)
which aws

CICD準備 & S3作成 $ EC2作成

参考

pages.awscloud.com

docs.aws.amazon.com

S3

バケット

作成

* バケット名は世界で唯一の名前に設定

作成確認

プロパティ

静的Webサイトホスティング

有効にする

インデックスドキュメント及びエラードキュメント設定

アクセス設定

バケットポリシー設定

 xxxxxxxxに作成したバケット名を指定
{
  "Version": "2012-10-17",
  "Statement": [
      {
          "Sid": "PublicReadGetObject",
          "Effect": "Allow",
          "Principal": "*",
          "Action": [
              "s3:GetObject"
          ],
          "Resource": [
              "arn:aws:s3:::xxxxxxxx/*"
          ]
      }
  ]
}

アップロード

ファイルをアップロード

アクセス

確認

IAM

ロール

作成

  • AmazonEC2RoleforAWSCodeDeploy
  • EC2InstanceConnect

EC2

インスタンス

作成

  • 名前はCodeDeployの設定時に必要なため控えること

セキュリティグループ

httpのアクセス許可

  • インバウンドのセキュリティグループをクリック
  • セキュリティグループIDをクリック
  • インバウンドのルールを編集
  • ルールを追加をクリック
  • SSHはマイIPとAWSIPアドレスを指定
  • HTTPにマイIPを指定
curl -s https://ip-ranges.amazonaws.com/ip-ranges.json| jq -r '.prefixes[] | select(.region=="ap-northeast-1") | select(.service=="EC2_INSTANCE_CONNECT") | .ip_prefix'

インスタンス

確認

  • 対象インスタンスの状態が実行中であることを確認

    IAM変更

  • 作成したIAMロールを指定

CodeDeploy-Agent

インストール

sudo yum update
sudo yum install ruby wget -y
cd /home/ec2-user
wget https://aws-codedeploy-eu-central-1.s3.eu-central-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
systemctl status codedeploy-agent
sudo systemctl start codedeploy-agent

httpd

インストール

sudo dnf install httpd -y
sudo systemctl start httpd.service

手元PC

ブラウザ

EC2にアクセス