~ コード化の開始から頻出パターンまで ~ 大村幸敬 Amazon Web Services ソリューションアーキテクト
Agenda
Who am I? 大村幸敬 ( おおむらゆきたか ) Solutions Architect @Amazon Web Services Japan エンタープライズのお客さまを担当 Management Tools & DevOps 系サービスを担当 好きなサービス : AWS CLI
"Are you Well-Architected? Werner Vogels
その運用は Well-Architected か? アプリケーション アジャイル開発で要件の変化に追従可能 インフラ クラウドで初期投資なく迅速に変更可能 運用 申請書と手順書と人海戦術で迅速に変更可能?
Well-Architected Operational Excellence の柱 原則 1. コードを使って運用する 2. 注記付きドキュメントを自動生成する 3. 頻繁に 小さく 可逆的に変更する 4. 運用手順を頻繁に見直す 5. 危険の事前予測と排除を行う 6. 全ての運用の失敗から学ぶ Well-Architected Framework = AWS 利用におけるベストプラクティス https://aws.amazon.com/jp/architecture/well-architected/
コードを使った運用のポイント from AWS Well-Architected sh 1. コードで全ての構成を定義 sh 2. イベントに対してスクリプトで対処 3. アプリケーションと同じ手法でコードを開発
1. コードで全ての構成を定義 同じ環境を 迅速に 繰り返し作成可能 doc sh sh sh sh
2. イベントに対してスクリプトで対処 自動的に 同じ処理を 繰り返し実施可能 sh sh sh sh sh sh sh sh
3. アプリケーションと同じ手法でコードを開発 コードと作成した環境の品質を担保 sh sh CI/CD*
コードを使った運用のポイント from AWS Well-Architected sh 1. コードで全ての構成を定義 同じ環境を 迅速に 多数作成可能 sh 2. イベントに対してスクリプトで対処 自動的に 同じ処理を 繰り返し実施可能 3. アプリケーションと同じ手法でコードを開発 コードと作成した環境の品質を担保
全ての構成変更はサービス利用者に影響 ver2.0? *M/W = ミドルウェア
AWS 環境に対するオペレーション方法 AWS
AWS のプロビジョニングサービスのカバー範囲 App デプロイ M/W OS EC2 他のサービス
デプロイ対象による管理範囲の違い ユーザ管理 AWS 管理 App デプロイ M/W OS EC2 コンテナ デプロイ Fargate App デプロイ Lambda
API へのアクセスと OS へのアクセス VPC 作成 ブラウザ CLI / SDK HTTPS HTTPS マネジメントコンソール AWS API AWS IAM ( 認証と認可 ) セキュリティグループ設定 EC2 作成 S3 バケットの作成 S3 オブジェクトへのアクセス EC2 RDP SSH ターミナル リモートデスクトップ OS へのアクセス方法はオンプレと同様
AWS 環境を管理するためのツール
CLI と CFn を使った AWS 環境操作の流れ sh
AWS CloudFormation AWS CloudFormation 作成 / 変更 / 削除 AWS リソースの作成 / 変更 / 削除 テンプレート - 作成するリソースの定義 - JSON/YAML スタック - AWS リソースの集合
CloudFormation 基本機能
AWS CLI aws <service> <operation> $ aws ec2 describe-instances $ aws s3 cp hogehoge s3://bucket/key/to/file
AWS CLI 使いどころ sh
AWS CLI CloudFormation
CloudFormation Tips CI/CD* * CI/CD = Continuous Integration / Continuous Deploy
CloudFormation Tips CI/CD
テンプレートの基礎
テンプレート開発の Tips
[IDE] AWS Cloud9 ブラウザベース Syntax ハイライト キーワード補完 Linux シェル環境 AWS CLI & IAM Role セットアップ済み 各種開発ツール
[Parameters] AWS Systems Manager - Parameter Store aws ssm put-parameter --name /MySystem/Prod/DB/User --type String --value myuser aws ssm get-parameter --name /MySystem/Prod/DB/User { "Parameter": { "Name": "/MySystem/Dev/DB/User", "Type": "String", "Value": "ohmurayu", "Version": 1 } }
[Parameters] AWS Systems Manager - Parameter Store 自身で設定した値を使用 Parameters: Env: Type: AWS::SSM::Parameter::Value<String> Default: /MySystem/Prod/DB/User AWS が提供する値を使用 Parameters: MyAMIID: Type : AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> Default: /aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2 # Default: /aws/service/ami-windows-latest/windows_server-2016-english-full-base 注 :SecureString は 2018/6/1 時点で未対応
[Test] 各種ツールによるコードのチェック https://github.com/awslabs/cfn-python-lint https://github.com/stelligent/cfn_nag cfn-python-lint + VS Code cfn_nag $ cfn_nag_scan -i public_alb.yaml ------------------------------------------------------------ public_alb.yaml ------------------------------------------------------------ WARN W2 Resources: ["PublicAlbSecurityGroup"] Security Groups found with cidr open to world on ingress. This should never be true on instance. Permissible on ELB Failures count: 0 Warnings count: 1
CloudFormation Tips CI/CD
スタック分割の例 サンプルシステム
スタック分割 - 基本方針
スタック分割の例 スタック構成
CloudFormation Tips CI/CD
CI/CD* for CloudFormation アプリケーション開発と同じやり方で環境全体を開発 sh * CI/CD = Continuous Integration / Continuous Deploy
AWS CloudFormation Validation Pipeline https://aws.amazon.com/jp/answers/devops/aws-cloudformation-validation-pipeline/
AWS CloudFormation Validation Pipeline
AWS CLI Tips SA ML
AWS CLI Tips SA ML
AWS CLI の利用開始 $ pip install awscli --upgrade --user $ aws help
CLI オペレーション Tips $ aws help $ aws ec2 help $ complete -C '/usr/local/bin/aws_completer' aws
CLI Tips - query $ aws ec2 describe-security-groups { "SecurityGroups": [ { "Description": "HTTPS+HTTP+ssh", "GroupName": "default", "IpPermissions": [ { "FromPort": 80, $ aws ec2 describe-security-groups --query "SecurityGroups[].[GroupId, GroupName]" --output text sg-bf8289da default sg-bf9256c6 op-ad-sg sg-c4f005bd rdp-http sg-cb5953ae yu-http ~/.aws/cli/alias list-sgs = ec2 describe-security-groups --query "SecurityGroups[].[GroupId, GroupName]" --output text $ aws list-sgs sg-bf8289da default sg-bf9256c6 op-ad-sg
AWS CLI Tips SA ML
CLI アクセス経路 オンプレミスから VPC エンドポイントへのアクセス可否は AWS サービスと接続形態により異なる
AWS CLI Tips SA ML
CLI 認証 & 認可 - AWS 認証情報
CLI 認証 & 認可 - 認証情報へのアクセス
CLI & -
CLI & - ~/.aws/config [profile admin-org-account] aws_access_key_id = AKIDF... aws_secret_access_key = 1rHl... [profile admin-team-account] source_profile = admin-org-account role_arn = arn:...:role/admin-team-account $ aws s3 ls --profile admin-team-account
CLI 認証 & 認可 SAML フェデレーションアクセス AssumeRoleWithSAML LDAP identity store Identity provider (IdP) role awsprocesscreds-saml https://github.com/awslabs/awsprocesscreds SAML Idp(ADFS や Okta) にログインして CLI に認証情報を渡すツール
CLI 認証 & 認可 - フェデレーションアクセス ~/.aws/config [profile saml] credential_process = awsprocesscreds-saml -e 'https://corp.example.com/adfs/ls/idpinitiatedsignon.aspx?logintorp=urn:amazon:webservices' -u username -p adfs -a arn:aws:iam::123456789012:role/adfs-dev コマンド実行 awsprocesscreds-saml アクセスキー AWS CLI $ aws s3 ls --profile saml { AccessKeyId : ADIF..., SecretAccessKey : 2rFt..., Version : 1 } awsprocesscreds-saml
CLI & MFA {"Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": {"AWS": {"arn:aws:iam::...:user/normal-user"}}, "Action": "sts:assumerole", "Condition": {"Bool": {"aws:multifactorauthpresent": "true"}} }]} ~/.aws/config [profile normal-access] aws_access_key_id = AKIDF... aws_secret_access_key = 1rHl... [profile mfa-access] source_profile = normal-access role_arn = arn:...:role/mfa-access-role mfa_serial = arn:...:mfa/mfa-of-user $ aws s3 ls s3://public-bkt/ --profile normal-access... $ aws s3 ls s3://restrict-bkt/ --profile mfa-access Enter MFA code for arn:...:mfa/mfa-of-user...
-
Organizations アカウント払い出し (CLI) organizations create-account organizations list-create-account-status iam create-role iam put-role-policy organizations list-roots organizations list-organizational-units-for-parent organizations list-parents organizations move-account
複数アカウントへの展開 - CloudFormation StackSet 管理者アカウント StackSet ターゲットアカウント A ターゲットアカウント B ターゲットアカウント A ターゲットアカウント B Stack Stack Stack Stack アカウント C アカウント D アカウント E アカウント C アカウント D アカウント E Region Region
CloudFormation StackSet - テンプレートの選択
CloudFormation StackSet - 適用範囲の指定
AWS Management Tools
EC2 CloudWatch Event Status = Pending Lambda
JSON/YAML 変換は cfn-flip $ cfn-flip vpc.json > vpc.yaml
テンプレートエンジンを使用したテンプレート作成
更新頻度と役割分担によってスタックを分ける (Cross Stack Reference) 1Export を使って他のスタックから参照したい値をエクスポート例 : セキュリティグループ ID 3ImportValue 関数を使って値を参照する 2 別のスタックから参照可能な値がエクスポートされる
テンプレート分割とスタック分割
テンプレート分割とスタック分割 推奨ユースケース Nested Stacks ( テンプレートの分割 ) テンプレートの再利用 1 つのスタックを管理するために複数のテンプレートを使用する Cross Stack References ( スタックの分割 ) 共用リソースのシェア 個別のスタックを個別のライフサイクルで独立管理できる 利点 管理が容易 1 つのスタックで全てを管理する 作成順序と依存関係が記述できる 利害関係の分離 DB や VPC の共用 障害時影響範囲の限定 考慮点 更新やロールバックの影響範囲が大きい カスタムリソース名を持つテンプレートの再利用に注意 リプレースアップデートになると物理 ID が変化するため 参照側のスタックの更新が必要 個別スタックの作成順序を管理する必要あり
専用 CLI
CLI S3 - Thread 1 Thread 2 Thread 3 IO Queue IO Thread Thread pool Amazon S3 bucket AWS CLI Disk
CLI S3 - ~/.aws/config [default] region = ap-northeast-1 s3 = max_concurrent_requests = 20 multipart_chunksize = 16MB multipart_threshold = 64MB max_queue_size = 10000
max_concurrent_requests = 4 Thread 1 Thread 2 Thread 3 IO Queue IO Thread Thread 4 Thread pool Amazon S3 bucket AWS CLI Disk
参考資料