📝

CloudShell から AWS CLI のみで Amplify アプリを作成してみた

に公開

Deploying an application to Amplify without a Git repository - AWS Amplify Hosting
Git を使用しない方法で試してみました。

手順

# デプロイするアプリを作成
$ mkdir hello-world-app
$ cd hello-world-app

$ cat <<EOF > index.html
<!DOCTYPE html>
<html>
<head>
  <title>Hello</title>
</head>
<body>
  <h1>Hello World</h1>
</body>
</html>
EOF

$ zip hello-world-app.zip index.html

# S3 バケットへアップロード
$ BUCKET_NAME=my-hello-world-$(date +%s)
$ aws s3 mb s3://$BUCKET_NAME
$ aws s3 cp hello-world-app.zip s3://$BUCKET_NAME/

# バケットポリシー設定
$ aws s3api delete-public-access-block \
--bucket $BUCKET_NAME

$ ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
$ REGION=$(aws configure get region)
$ cat <<EOF > bucket-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowPublicRead",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::$BUCKET_NAME/*"
    },
    {
      "Sid": "AllowAmplifyServiceAccess",
      "Effect": "Allow",
      "Principal": { "Service": "amplify.amazonaws.com" },
      "Action": ["s3:ListBucket", "s3:GetObject"],
      "Resource": [
        "arn:aws:s3:::$BUCKET_NAME",
        "arn:aws:s3:::$BUCKET_NAME/*"
      ],
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "$ACCOUNT_ID",
          "aws:SourceArn": "arn:aws:amplify:$REGION:$ACCOUNT_ID:apps/$APP_ID/branches/main"
        }
      }
    }
  ]
}
EOF

$ aws s3api put-bucket-policy \
--bucket $BUCKET_NAME \
--policy file://bucket-policy.json

$ aws s3api put-public-access-block \
--bucket $BUCKET_NAME \
--public-access-block-configuration '{
  "BlockPublicAcls": true,
  "IgnorePublicAcls": true,
  "BlockPublicPolicy": true,
  "RestrictPublicBuckets": true
}'

# Amplify アプリ作成
$ cat <<EOF > custom-rules.json
[
  {
    "source": "/<*>",
    "target": "/index.html",
    "status": "404-200"
  }
]
EOF

$ aws amplify create-app \
--name HelloWorldApp \
--platform WEB \
--custom-rules file://custom-rules.json

# Amplify ブランチ作成
$ APP_ID= <create-app に含まれる appId>
$ aws amplify create-branch \
--app-id $APP_ID \
--branch-name main \
--stage PRODUCTION

# デプロイ
$ aws amplify start-deployment \
--app-id $APP_ID \
--branch-name main \
--source-url s3://$BUCKET_NAME/hello-world-app.zip \
--source-url-type ZIP

# 動作確認
$ echo "https://$BRANCH_NAME.$APP_ID.amplifyapp.com/"
https://main.d2ilvsidty8ocn.amplifyapp.com/

$ curl https://main.d2ilvsidty8ocn.amplifyapp.com/
<!DOCTYPE html>
<html>
<head>
  <title>Hello</title>
</head>
<body>
  <h1>Hello World</h1>
</body>
</html>

まとめ

今回は CloudShell から AWS CLI のみで Amplify アプリを作成してみました。
どなたかの参考になれば幸いです。

参考資料

Discussion