(続)静的Webサイトホスティング二重化してみる(その4)

前回までで おおよそ意図した構成は完成したものの。やっぱりコンテンツを二重投稿するのはイヤだということでGCSのときと同様にGitHubActions使ってmainブランチにコンテンツをpushするとS3Blobに同時にコンテンツをアップロードするという構成を作ります。

AWSのIAMでS3にオブジェクトアップロードできる権限を作るとことGitHubActionsの設定やなんかはGCSのときのを参考にシークレットとか設定します。


最終的に↓感じの構成になりました。








◆シリーズ

その1:AWSでCloudFront+S3構成を作る

その2:AzureでApplicationGateway+Blob構成を作る

その3:DNSでフェイルオーバー設定

その4:S3とBlobにGitHubActions使ってコンテンツ同期(今回)

その5:TerraformでIaC化

◆環境

azure-cli  2.14.0 
Ubuntu20.04.4LTS

◆作業内容

1.AzureのCREDENTIALSを発行

ローカルで発行しようとしたもののエラーが発生したて解消できませんでした。どんだけあがいてもうまく行かなかったのでAzureのCloudShell上でコマンドを叩きます。
$ az ad sp create-for-rbac --name tohonokai_tk --role contributor \
                            --scopes /subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP_NAME} \
                            --sdk-auth
The underlying Active Directory Graph API will be replaced by Microsoft Graph API in Azure CLI 2.37.0. Please carefully review all breaking changes introduced during this migration: https://docs.microsoft.com/cli/azure/microsoft-graph-migration
Option '--sdk-auth' has been deprecated and will be removed in a future release.
Creating 'contributor' role assignment under scope '/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP_NAME}'
The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control. For more information, see https://aka.ms/azadsp-cli
{
  "clientId": "${YOUR_CLIENT_ID}",
  "clientSecret": "${YOUR_CLIENT_SECRET}",
  "subscriptionId": "${SUBSCRIPTION_ID}",
  "tenantId": "${YOUR_TENANT_ID}",
  "activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
  "resourceManagerEndpointUrl": "https://management.azure.com/",
  "activeDirectoryGraphResourceId": "https://graph.windows.net/",
  "sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
  "galleryEndpointUrl": "https://gallery.azure.com/",
  "managementEndpointUrl": "https://management.core.windows.net/"
}

※ローカルのAzure-CLIで叩いた時のエラーです。解決法ご存じの方いらっしゃったらご教示いただけると幸いです。
Changing "rbac_tohonokai_tk" to a valid URI of "http://rbac_hoge", which is the required format used for service principal names
Values of identifierUris property must use a verified domain of the organization or its subdomain: 'http://rbac_hoge'

2.Secret設定

GitHubのリポジトリ、[Settings]でSecretを以下の通り設定する。
AWS_ACCESS_KEY_ID       AWSのAccess key ID
AWS_SECRET_ACCESS_KEY     AWSのSecret access key
S3_UPLOAD_BUCKET        AWSのS3バケット
STORAGE_ACCOUNT_NAME           blobのストレージアカウント
AZURE_CREDENTIALSには以下の通りCLIのコマンドの出力結果JSONをそのまま貼り付ける
{
  "clientId": "${YOUR_CLIENT_ID}",
  "clientSecret": "${YOUR_CLIENT_SECRET}",
  "subscriptionId": "${SUBSCRIPTION_ID}",
  "tenantId": "${YOUR_TENANT_ID}",
  "activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
  "resourceManagerEndpointUrl": "https://management.azure.com/",
  "activeDirectoryGraphResourceId": "https://graph.windows.net/",
  "sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
  "galleryEndpointUrl": "https://gallery.azure.com/",
  "managementEndpointUrl": "https://management.core.windows.net/"
}


3.GitHubActionsファイルの準備

・ディレクトリ準備

$ git clone [Your Git Repo] html
$ cd html
$ mkdir dist
$ mkdir .github
$ mkdir .github/workflows
#distディレクトリにindex.htmlと404.htmlをアップロード
$ cd .github/workflows
$ tree -a --charset=C
.
|-- .git
|-- .github
|   `-- workflows
|-- .gitignore
`-- dist
    |-- 404.html
    `-- index.html

・YAMLファイル準備

$ vi staticsite_deploy.yaml
--------------------------
name: Website - Production
on:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-1

      - name: Sync to S3
        env:
          S3_UPLOAD_BUCKET: ${{ secrets.S3_UPLOAD_BUCKET }}
        run: |
          aws s3 sync --delete dist s3://$S3_UPLOAD_BUCKET/

      - name: Configure Azure credentials
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Sync to Blob
        env:
          STORAGE_ACCOUNT_NAME: ${{ secrets.STORAGE_ACCOUNT_NAME }}
        uses: azure/CLI@v1
        with:
          inlineScript: |
            az storage blob upload-batch --account-name $STORAGE_ACCOUNT_NAME --auth-mode key -d '$web' -s dist
--------------------------------

・GitHubにPush

$ cd ../..
$ git add .
$ git commit -m '2022-04-23 GitHubActions'
$ git push origin main


◆参考サイト

・AzureのBlobのGitHubActions設定


・サブスクリプションID取得方法



ほとんどMicrosoft公式に書いてある手順でやれちゃいました。敢えて記事にするほどのこともなかったんじゃないかと思いますが、備忘録かねて残しておきます。
これでやりたいことは全部できたんですが、GCPの時はTerraform化してるんですよねぇ・・。でもまぁAzureはこれでいいかなと思うのでここまでで完結です。
気が向いたらTerraformも手を付けてみたりかもですが、やるかどうかは未定です。

コメント

このブログの人気の投稿

証券外務員1種勉強(計算式暗記用メモ)

GASでGoogleDriveのサブフォルダとファイル一覧を出力する

マクロ経済学(IS-LM分析)