LoginSignup
0
0

More than 1 year has passed since last update.

AWS CLIで Web サイトを構築、管理、運用する(21日目)

Posted at

21 日目!

予告通り、コンテンツ周り、特に管理やデプロイについて設定をいれていきます。

21日目の要約

コンテンツをリポジトリで管理するよ!

AWS CLI の準備

このあたりをみて、好きなバージョンとお使いのOSにあった環境設定をしてくださいね。
なんなら、 AWS CloudShell で実行するのも楽でよいと思います。
この記事シリーズは、AWS CloudShell で実行し、実行例を載せています。

バージョン1
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-cliv1.html

バージョン2
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-cliv2.html

概要

CodeCommit を使ってコンテンツを管理するよ!

さあ、やってみよう!

リポジトリを作る

CodeCommit のリポジトリを作成して、コンテンツを管理できるようにします。

aws codecommit create-repository --repository-name <任意のリポジトリ名>

リポジトリが作れると以下の json が返ってきます。

{
    "repositoryMetadata": {
        "accountId": "************",
        "repositoryId": "********-****-****-****-************",
        "repositoryName": "<指定したリポジトリ名>",
        "lastModifiedDate": "2021-12-20T16:53:42.654000+00:00",
        "creationDate": "2021-12-20T16:53:42.654000+00:00",
        "cloneUrlHttp": "https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/<指定したリポジトリ名>",
        "cloneUrlSsh": "ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/<指定したリポジトリ名>",
        "Arn": "arn:aws:codecommit:ap-northeast-1:************:<指定したリポジトリ名>"
    }
}

git のセットアップを行う

以下の例は AWS CloudShell での実施したものです。

ローカルリポジトリを作成します。
合わせて、 S3 に格納した index.html の元ファイルをコピーしておきます。

mkdir <リポジトリ名>
cp -p index.html <リポジトリ名>/index.html
cd <リポジトリ名>
git init

以下が出力されました。

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /home/cloudshell-user/<リポジトリ名>/.git/

git コマンドで CodeCommit 上のリポジトリが扱えるように設定します。

git config --local credential.helper '!aws codecommit --region ap-northeast-1  credential-helper $@'
git config --local credential.UseHttpPath true
git remote add origin https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/<リポジトリ名>
git config --global user.email "<メールアドレス>"
git config --global user.name "<ユーザー名>"

設定が終わったら、index.html ファイルをリポジトリにプッシュします。

git add index.html 
git commit -m "First Commit"
git push origin master

正常にプッシュできると以下のように表示されます。
※以下の例は2回目のプッシュのため、一回目とは若干異なる

[master *******] test
 1 file changed, 1 insertion(+), 1 deletion(-)
git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/<リポジトリ名>
   23*****..a6*****  master -> master

これで CodeCommit を使ったコンテンツ管理ができるようになりました。

まとめ

今までは ローカルのみでコンテンツを管理していましたが、 CodeCommit を使うことで変更履歴管理を行えるようになりました。

明日は、コンテンツがプッシュされたら S3 へデプロイされるように設定していきます。

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