{Proton}AWS CLI の開始方法


https://docs.aws.amazon.com/ja_jp/proton/latest/userguide/ag-getting-started-cli.html

https://dev.classmethod.jp/articles/aws-proton-general-availability/


AWS Proton は、プラットフォームエンジニアがセルフサービスのデプロイと更新のために
インフラストラクチャテンプレートを使用してプロセスを拡張できる完全マネージド型サービスです。

 

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

 

-- 1.2 jqインストール
sudo yum -y install jq

-- 1.3 gitインストール
sudo yum -y install git

 


-- 2. ロールの作成

※マニュアルでは下記を区別しているが、相違がないので区別せずにロールを作成する
AWS Proton service role
AWS Proton pipeline service role

-- 2.1 ポリシーの作成


vim policy01.json

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:CancelUpdateStack",
                "cloudformation:ContinueUpdateRollback",
                "cloudformation:CreateChangeSet",
                "cloudformation:CreateStack",
                "cloudformation:DeleteChangeSet",
                "cloudformation:DeleteStack",
                "cloudformation:DescribeChangeSet",
                "cloudformation:DescribeStackDriftDetectionStatus",
                "cloudformation:DescribeStackEvents",
                "cloudformation:DescribeStackResourceDrifts",
                "cloudformation:DescribeStacks",
                "cloudformation:DetectStackResourceDrift",
                "cloudformation:ExecuteChangeSet",
                "cloudformation:ListChangeSets",
                "cloudformation:ListStackResources",
                "cloudformation:UpdateStack"
            ],
            "Resource": "arn:aws:cloudformation:*:999999999999:stack/AWSProton-*"
        },
        {
            "Effect": "Allow",
            "NotAction": [
                "organizations:*",
                "account:*"
            ],
            "Resource": "*",
            "Condition": {
                "ForAnyValue:StringEquals": {
                    "aws:CalledVia": ["cloudformation.amazonaws.com"]
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "organizations:DescribeOrganization",
                "account:ListRegions"
            ],
            "Resource": "*",
            "Condition": {
                "ForAnyValue:StringEquals": {
                    "aws:CalledVia": ["cloudformation.amazonaws.com"]
                }
            }
        }
    ]
}

aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json


-- 2.2 ロールの作成

vim role01.json

{
    "Version": "2012-10-17",
    "Statement": {
        "Sid": "ServiceTrustRelationshipWithConfusedDeputyPrevention",
        "Effect": "Allow",
        "Principal": {"Service": "proton.amazonaws.com"},
        "Action": "sts:AssumeRole"
    }
}


★マニュアル記載のConditionがあるとエラーとなる


aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json

-- 2.3 ポリシーをロールにアタッチ

aws iam attach-role-policy --policy-arn arn:aws:iam::999999999999:policy/policy01 --role-name role01

 


-- 3. CodeStar接続の作成

aws codestar-connections list-connections

aws codestar-connections create-connection \
--provider-type GitHub \
--connection-name con01

 

 

-- 4. 環境テンプレートを登録する

-- 4.1 下記リポジトリをフォークする

https://github.com/aws-samples/aws-proton-cloudformation-sample-templates/

 


-- 4.2 リポジトリリンクを作成する

aws proton create-repository \
--name username/aws-proton-cloudformation-sample-templates \
--connection-arn "arn:aws:codestar-connections:ap-northeast-1:999999999999:connection/11111111-1111-1111-1111-111111111111" \
--provider "GITHUB"

aws proton list-repositories

aws proton get-repository \
--name username/aws-proton-cloudformation-sample-templates \
--provider "GITHUB"

 

コンソールを見るとステータスが保留中となっている。
→「保留中の接続を更新」から指示通りに設定


-- 4.3 アカウント設定を構成


aws proton update-account-settings \
--pipeline-provisioning-repository '{
            "branch": "main",
            "name": "username/aws-proton-cloudformation-sample-templates",
            "provider": "GITHUB"
        }' \
--pipeline-service-role-arn arn:aws:iam::999999999999:role/role01


aws proton get-account-settings

 


-- 4.4 環境テンプレートを作成する


aws proton create-environment-template \
--name fargate-env \
--display-name fargate-env \
--description "fargate-env"

aws proton list-environment-templates


-- 4.5 環境テンプレート同期設定を作成する

aws proton create-template-sync-config \
--template-name fargate-env \
--template-type "ENVIRONMENT" \
--repository-name username/aws-proton-cloudformation-sample-templates \
--repository-provider "GITHUB" \
--branch "main" \
--subdirectory "environment-templates/fargate-env"

aws proton get-template-sync-config \
--template-name fargate-env \
--template-type "ENVIRONMENT"


-- 4.6 環境テンプレートバージョンを作成する

※バージョンが作成されない場合、GitHubのルートにあるREADME.mdをコミットするよい模様

aws proton wait environment-template-version-registered \
--template-name fargate-env \
--major-version "1" \
--minor-version "0"

 

aws proton update-environment-template-version \
--template-name fargate-env \
--major-version "1" \
--minor-version "0" \
--status "PUBLISHED"

aws proton list-environment-template-versions \
--template-name fargate-env

aws proton get-environment-template-version \
--template-name fargate-env \
--major-version "1" \
--minor-version "0"

 

-- 5. サービステンプレートを登録する

-- 5.1 サービステンプレートを作成する

aws proton create-service-template \
--name load-balanced-fargate-svc \
--display-name load-balanced-fargate-svc \
--description "load-balanced-fargate-svc"

aws proton list-service-templates


-- 5.2 サービステンプレート同期設定を作成する

aws proton create-template-sync-config \
--template-name load-balanced-fargate-svc \
--template-type "SERVICE" \
--repository-name username/aws-proton-cloudformation-sample-templates \
--repository-provider "GITHUB" \
--branch "main" \
--subdirectory "service-templates/load-balanced-fargate-svc"

 

aws proton get-template-sync-config \
--template-name load-balanced-fargate-svc \
--template-type "SERVICE"


-- 5.3 サービステンプレートバージョンを作成する


aws proton wait service-template-version-registered \
--template-name load-balanced-fargate-svc \
--major-version "1" \
--minor-version "0"

aws proton update-service-template-version \
--template-name load-balanced-fargate-svc \
--major-version "1" \
--minor-version "0" \
--status "PUBLISHED"


aws proton list-service-template-versions \
--template-name load-balanced-fargate-svc

aws proton get-service-template-version \
--template-name load-balanced-fargate-svc \
--major-version "1" \
--minor-version "0"

 

 

-- 6. 環境をデプロイする

-- 6.1 登録した環境テンプレートのサンプルスペックファイルを入手する

environment-templates/fargate-env/spec/spec.yaml


vim spec.yaml

proton: EnvironmentSpec
spec:
  vpc_cidr: 10.0.0.0/16
  public_subnet_one_cidr: 10.0.0.0/18
  public_subnet_two_cidr: 10.0.64.0/18
  private_subnet_one_cidr: 10.0.128.0/18
  private_subnet_two_cidr: 10.0.192.0/18

 

-- 6.2 環境を作成する

aws proton create-environment \
--name env01 \
--template-name "fargate-env" \
--template-major-version 1 \
--proton-service-role-arn arn:aws:iam::999999999999:role/role01 \
--spec file://spec.yaml \
--component-role-arn  arn:aws:iam::999999999999:role/role01


aws proton wait environment-deployed --name env01


aws proton list-environments

aws proton get-environment --name env01

 


-- 7. サービスをデプロイする [アプリケーション開発者]

-- 7.1 登録したサービステンプレートのサンプルスペックファイルを入手する

service-templates/load-balanced-fargate-svc/spec/spec.yaml

vim spec.yaml

proton: ServiceSpec
pipeline:
  service_dir: ecs-static-website
  dockerfile: Dockerfile
  unit_test_command: echo 'add your unit test command here'
instances:
  - name: load-balanced-fargate-svc-prod
    environment: env01


★environment名は環境名と合わせる必要がある


-- 7.2 下記リポジトリをフォークする

https://github.com/aws-samples/aws-proton-sample-services/

-- 7.3 サービスを作成する

aws proton create-service \
--name svc01 \
--repository-connection-arn "arn:aws:codestar-connections:ap-northeast-1:999999999999:connection/11111111-1111-1111-1111-111111111111" \
--repository-id username/aws-proton-sample-services \
--branch-name "main" \
--template-major-version 1 \
--template-name load-balanced-fargate-svc \
--spec file://spec.yaml

aws proton wait service-created --name svc01

aws proton list-services


-- 7.4 動作確認

aws proton list-service-instances

aws proton list-service-instance-outputs \
--service-name svc01 \
--service-instance-name load-balanced-fargate-svc-prod

 

 

-- 8. クリーンアップ

-- サービスの削除
aws proton list-services

aws proton delete-service \
--name svc01


-- 環境の削除
aws proton list-environments

aws proton delete-environment \
--name env01

-- CloudFormationスタックの削除確認


-- サービステンプレートバージョンの削除

aws proton list-service-template-versions \
--template-name load-balanced-fargate-svc

aws proton delete-service-template-version \
--template-name load-balanced-fargate-svc \
--major-version "1" \
--minor-version "0"

 

-- サービステンプレート同期設定の削除

aws proton get-template-sync-config \
--template-name load-balanced-fargate-svc \
--template-type "SERVICE"

aws proton delete-template-sync-config \
--template-name load-balanced-fargate-svc \
--template-type "SERVICE"

-- サービステンプレートの削除

aws proton list-service-templates

aws proton delete-service-template \
--name load-balanced-fargate-svc

 

-- 環境テンプレートバージョンの削除

aws proton list-environment-template-versions \
--template-name fargate-env

aws proton delete-environment-template-version \
--template-name fargate-env \
--major-version "1" \
--minor-version "0"


-- 環境テンプレート同期設定の削除

aws proton get-template-sync-config \
--template-name fargate-env \
--template-type "ENVIRONMENT"

aws proton delete-template-sync-config \
--template-name fargate-env \
--template-type "ENVIRONMENT"

 

-- 環境テンプレートの削除

aws proton list-environment-templates


aws proton delete-environment-template \
--name fargate-env

 

-- リポジトリリンクの削除

aws proton list-repositories

aws proton delete-repository \
--name username/aws-proton-cloudformation-sample-templates \
--provider "GITHUB"

 

-- CodeStar接続の削除

aws codestar-connections list-connections

aws codestar-connections delete-connection \
--connection-arn arn:aws:codestar-connections:ap-northeast-1:999999999999:connection/11111111-1111-1111-1111-111111111111


-- ロールの削除
aws iam list-roles | grep role01

aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::999999999999:policy/policy01

aws iam delete-role --role-name role01

 

-- ポリシーの削除
aws iam list-policies | grep policy01

aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01

 

-- S3の削除


-- フォークしたリポジトリの削除