ðæ¬è¨äºã¯ãAWS CDK Advent Calendar 2023 ã® 18æ¥ç®ã®è¨äºã¨ãªãã¾ã
AWSã®ãµã¼ãã¹ãå¦ã¶ã¨ãã«ãã³ãºãªã³ãè¡ããã¨ã¯å¤ãã¨æãã¾ãããæè¿ã¯åºæ¬çã«CDKã§å®è£ ãã¦ãã¾ãã ã¡ãªãããå ·ä½çãªæµããç°¡åã«ã¾ã¨ãã¾ãã
- ãã³ãºãªã³ãCDKã§å®è£ ããããã«ãã¦ããèæ¯
- ã¡ãªãã
- ãã¡ãªãã
- ãã³ãºãªã³ãCDKã§å®è£ ããã¨ãã®æµã
- å®éã«ãã£ã¦ã¿ã
- çµããã«
ãã³ãºãªã³ãCDKã§å®è£ ããããã«ãã¦ããèæ¯
AWSã®ãµã¼ãã¹ãå¦ã¶ã¨ãã«ãã³ãºãªã³ãWorkshopãããã³ã³ã§ãããããããã¨ã¯å¤ãã¨æãã®ã§ãããç§ã®å ´åãä½ãç¥ãããã©ã§ãããã¿ããã«ãªãé ã«æ®ããªããã¨ãå¤ã ããã¾ããã
â»ä»¥ä¸ã®è¨äºã§è¨åããã¦ãããè ¹è½ã¡åé¡ãã
çããã¯ããã³ãºãªã³ããå®æ½ããã¨ãã«ãåé¡ãªãåããã»ã»ã»! ã ãã©ã»ã»ã»! ãªãåãã¦ããã®ãã¯ããããªãã»ã»ã»!ãã¨æããçµé¨ã¯ããã¾ããã ? ç§ã¯ããããç¶æ ã®ãã¨ããè ¹è½ã¡åé¡ãã¨å人çã«å¼ãã§ãã¾ãã
ãããªã¨ãã«ä»¥ä¸ã®è¨äºãè¦ããã¾ããã
ãã®è¨äºãèªãã§ãããªãã»ã©ãªã¼ã¨æãããã³ãºãªã³ãåºæ¬çã«IaCï¼ç§ã®å ´åã¯CDKï¼ã§å®è£ ãã¦ã¿ããã¨ãç¶ãã¦ãã¾ãã
ã¡ãªãã
å人çã«æãã¦ããã¡ãªããã§ãã
ã¡ãªãã1. é ã«æ®ãããã
ãã³ãºãªã³ã¯ããã³ã³æä½ãªã©ã§æºåããã¦ãããã¨ãå¤ãã®ã§ãCDKã§å®è£ ãã¦ããéã«ããã³ã³ã®è¨å®é ç®ãConstructã®ããããã£ã«ãããã³ã°ãã¦ããã¨ããéç¨ãå¿ è¦ã«ãªãã¾ãããã®éç¨ã§è¨å®å¤ã®æå³ãç解ãã¦æ£ããè¨å®ããå¿ è¦ãããã®ã§ãé ã«æ®ããããã¨æãã¦ãã¾ã
ãã ããã©ã¤ã¢ã³ãã¨ã©ã¼ãå¿ è¦ã«ãªã£ããçããã®ã§ããã®åããã©ãæãããã¾ãï¼ãã®åé ã«æ®ãããããªãã¨æãã¾ãï¼ã
ã¡ãªãã2. ãªã½ã¼ã¹ã®åé¤ãåä½æã容æ
ãªã½ã¼ã¹ãä¸æ¦å ¨é¨åé¤ãããåä½æãããªã©ãç°¡åã«ã§ãã¾ãããããå½¹ç«ã¤å ´é¢ã¨ãã¦ã¯ä»¥ä¸2ã¤ã®ã±ã¼ã¹ãããããªã¨æãã¾ãã
- ãã³ãºãªã³ãä¸æãããã¨ãï¼ãã³ãºãªã³ã®ä¸ã§ã¯æéãè¦ãããã®ããããä¸æ¥ã§ããªããã¨ã¯å°é£ã§ä¸æããããã¨ãããã¾ãããã®å ´åã¯IaCã®ã¡ãªãããæ´»ããã¦ãä¸ææã«ä¸æ¦å ¨ãªã½ã¼ã¹ãæ¶ãã¦èª²éãé²æ¢ãå¾æ¥åéãããã¨ãã«åä½æã¨ãããã¨ã容æã«ã§ãã¾ãã
- ãªã½ã¼ã¹ã®æ¶ãå¿ãé²æ¢ï¼ãã³ãºãªã³ã§ä½æãããªã½ã¼ã¹ãæ¶ãå¿ãã¦èª²éããã¦ãã¾ã£ããã¨ããã®ã¯ããããã®ä¸ã¤ã ã¨æãã¾ããIaCã ã¨ã¾ã¨ãã¦åé¤ã¨ãããã¨ã容æãªã®ã§æ¶ãå¿ããæ¸ãããã¨ãã§ãã¾ããä¾ãã°CDKã§ããã°ãã³ãºãªã³ãçµãã£ãã
cdk destroy
ã§æ ¹ããããªã½ã¼ã¹ãåé¤ãããªã©ã
ã¡ãªãã3. ã³ã¼ããåå©ç¨ã§ãã
å®è£ ããCDKã®ã³ã¼ããèªåã®è³ç£ã¨ãã¦æ®ããã®ã§ãå°æ¥çã«åå©ç¨ãããã¨ãå¯è½ã§ããä½ããæ§ç¯ããéã«ããããã®ãã³ãºãªã³ã§è¿ããã®ä½ã£ããª...ãã¨æãåºãåå©ç¨ããã¨ãããã¨ãå¤ã ããã¾ãã
ãã¡ãªãã
ãã¡ãªãã1. ããã³ã³æä½ããæéãããã
ããã³ã³æä½çâCDKã®å®è£ ã«è½ã¨ãè¾¼ãä½æ¥ãå¿ è¦ãªã®ã§ããã®åããããããããæéããããã¾ãããã®ãããµãã¨è§¦ã£ã¦ãµã¼ãã¹ã®æ¦è¦ãææ¡ãããæ¹ã«ã¯åããªãããããã¾ããã
ç§ãç°¡æã«ãµã¼ãã¹ã®æ¦è¦ã確èªãããå ´åã¯ããã³ã³æä½ã§æ¸ã¾ããå ´åãããã¾ããã¡ãªããã®1~3ã®æ¹ãåãã¨æããå ´åã¯ãCDKã§å®è£ ãã¦ãã¾ãã
ãã¡ãªãã2. ããããCDKã§å®è£ ã§ããªãå ´åããã
CDKã§ãã³ãºãªã³ãè¡ãå ´åã大åæã¨ãã¦CDKãCloudFormationã対å¿ãã¦ããå¯è½æ§ãããã¾ãï¼CloudFormationãæä½é対å¿ãã¦ããã°L1 Constructã§å®è£ å¯è½ï¼ã
ãã®å ´åã¯ãã§ããç¯å²ã§CDKã§å®è£ ãã¦ä¸é¨ã¯ããã³ã³ã§æ§ç¯ or å ¨é¨ããã³ã³ã§æ§ç¯ãªã©ãè¡ãå¿ è¦ãããã¾ãã
ãã³ãºãªã³ãCDKã§å®è£ ããã¨ãã®æµã
ç§ãããããæµããè¨è¼ãã¾ãã
1. å ãã¿ãæ¢ã
å¦ã³ãããµã¼ãã¹ãåºã¦ããã¨ãã«ãå ãã¿ã¨ãªããã³ãºãªã³ãæ¢ãã¾ãã
ãµã¼ãã¹ã«ãã£ã¦ã¯è¤æ°è¦ã¤ãããã¨ãããã®ã§ãç®çã«é©ãããã®ãåæ¨é¸æãã¾ããåä¸ã®ãµã¼ãã¹ã«ã¤ãã¦æ·±ãå¦ã³ããã®ããçµã¿åãããå¦ã³ããã®ããããçã
ããµã¼ãã¹å + Workshopãã§ã°ã°ã
å¦ã³ãããµã¼ãã¹å + Workshop ã§ã°ã°ãã¨è©²å½ãããã®ãè¦ã¤ãããã¨ãå¤ãã§ãã
AWS Workshops
AWSå ¬å¼ã®ã¯ã¼ã¯ã·ã§ãããã¾ã¨ã¾ã£ã¦ãããµã¤ãã§ãããµã¼ãã¹åå¥ã®ã¯ã¼ã¯ã·ã§ãããªã©ãè±å¯ã§ãã workshops.aws
AWS æ¥æ¬èªãã³ãºãªã³
æ¥æ¬èªã§è¡ãããã³ãºãªã³ãã¾ã¨ã¾ã£ã¦ãããµã¤ãã§ãããã¡ãããã¾ã«ä½¿ãã¾ãã
2. GitHubã§ãªãã¸ããªãä½æãã
GitHubä¸ã§CDKããã¸ã§ã¯ããæ ¼ç´ãããªãã¸ããªãä½æãã¾ãã
3. CDKã§ãã³ãºãªã³ãå®è£ ãã¦ãã
ä¸çªã®é å¼µãã©ããã§ãããã³ãºãªã³ã®æé ãCDKã«è½ã¨ãè¾¼ã¿å®è£ ãã¦ããã¾ãã
4. READMEã«ã¢ã¼ããã¯ãã£å³ãWorkShopã®ãªã³ã¯ãã¤ãã¦ããã·ã¥
å®è£ ãçµãã£ããREADMEã«ã¢ã¼ããã¯ãã£å³ãWorkShopã®ãªã³ã¯ãç°¡åã«ã¤ãã¦ããã¾ãã WorkShopã¯ãã¾ã«ãªã³ã¯èªä½ãæ¶æ» ãããã¨ããã®ã§ãç°¡åã§ãã¢ã¼ããã¯ãã£å³ãã¤ãã¦ããã¨å¾ã 便å©ãã¨æãã¾ãã
ãªãç§ã¯ã¢ã¼ããã¯ãã£å³ã¯VSCodeã®Draw.ioæ¡å¼µæ©è½ã使ç¨ãã¦ä½æãããã¨ãå¤ãã§ãã
å®éã«ãã£ã¦ã¿ã
å®éã«WorkShopã使ç¨ãã¦ãå®è£ ããã¾ã§ã®è©³ç´°ãªæµãã以ä¸ã«ç¤ºãã¾ãã ãªããäºåæºåã¨ãã¦ãµã¼ãã¹ã®æ¦è¦ã®ç¥èãä»å ¥ãã¦ããããå ´åã¯BlackBeltã®åç»ãè¦ãçãã¦ããã¾ãã
ä»åç§ã¯ãããã¼ã¸ãã©ã³ã¿ã¤ã ã使ã£ãApp Runnerããå¦ç¿ãããã£ãã®ã§ããããå ã«ãã£ã¦ã¿ã¾ãã
ãªããå®éã«ä½æãããã®ã¯ä»¥ä¸ã«ãªãã¾ãã
1. å ãã¿ãæ¢ã
App Runner Workshop
ã§ã°ã°ã£ãã¨ããã以ä¸ã®WorkShopãè¦ã¤ãã£ãã®ã§ãã¡ãã§å®æ½ãã¦ããã¾ãã
ã³ã³ãã³ãããã£ã¨çºãã¦ãã©ã®ç¯å²ãå®æ½ããã決ãã¦ããã¾ãï¼ãã¡ããå ¨é¨ããå ´åãããï¼ã
ä»åã¯ããã¼ã¸ãã©ã³ã¿ã¤ã ã«é¢ãã¦å¦ç¿ãããã£ãã®ã§èµ¤æ ã®é¨åããã£ã¦ããã¾ãã
2. GitHubã§ãªãã¸ããªãä½æããã
ãµã¼ãã¹åãå«ãååãªã©ã§ãªãã¸ããªãä½æãã¾ãã
3. CDKã§ãã³ãºãªã³ãå®è£ ãã¦ãã
ã¡ã¤ã³ã®ã¨ããã§ãã
äºåæºåã®å®è£
ãã³ãºãªã³ã®ãããããã¿ã¼ã³ã¨ãã¦ã主é¡ã®ãµã¼ãã¹ä»¥å¤ã®é¨åã®CloudFormationãã³ãã¬ã¼ããæä¾ããã¦ãããæºåã¨ãã¦ãããå ã«ãããã¤ãããã¨ããã®ãããã¾ãã
ä»åã®WorkShopã§è¨ãã¨èµ¤ç·ã®ãªãã¸ããªã®ã¨ããã«ããµã³ãã«APã¨é¢é£ããCloudFormationãã³ãã¬ã¼ããå ¥ã£ã¦ãã¾ãã
CloudFormationãã³ãã¬ã¼ãã確èªããã¨ãããDynamoDBãApp Runnerç¨ã®IAMãã¼ã«ãå®ç¾©ããã¦ãã¾ãã
â»DynamoDBã®é¨åã®ã¿æç²ã
Resources: Table: Type: AWS::DynamoDB::Table Properties: TableName: repostatus_cache AttributeDefinitions: - AttributeName: repoSlug AttributeType: S KeySchema: - AttributeName: repoSlug KeyType: HASH BillingMode: PAY_PER_REQUEST TimeToLiveSpecification: AttributeName: ttl Enabled: true
ãã®äºåæºåé¨åã«ã¤ãã¦ãCDKã§æ¹ãã¦å®è£ ãããã¨ãå¤ãã§ããä»ãµã¼ãã¹å«ãã¦ä¸ã¤ã®ãã¶ã¤ã³ãã¿ã¼ã³ã¿ãããªå½¢ã§è³ç£ãæ®ããããã
ä»åã®DynamoDBã®å ´åã¯ä»¥ä¸ã®ããã«å®è£ ãã¦ãã¾ãï¼ä¸é¨ã«ã¹ã¿ãã¤ãºï¼ã
// DynamoDBã®ãã¼ãã«ãä½æ const dynamoDbTable = new dynamodb.Table(this, 'DynamoDB', { tableName: 'repostatus_cache', partitionKey: { name: 'repoSlug', type: dynamodb.AttributeType.STRING }, billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, timeToLiveAttribute: 'ttl', removalPolicy: cdk.RemovalPolicy.DESTROY // Stackåé¤æã«ãã¼ãã«ãæ¶ãããã«è¨å® });
ãã¨ã¯ãã³ãºãªã³æé ãCDKã®å®è£ ã«è½ã¨ãè¾¼ãã§ããã¾ãã
ä¾ãã°ä»¥ä¸ã®ã©ã³ã¿ã¤ã ããã³ãã³ããè¨å®ããé¨åã«é¢ãã¦è¨ãã¨
CDKã®å®è£ ã§ã¯ä»¥ä¸ã®â ã®é¨åã該å½ãã¾ã
â»ä»åApp Runnerã®å®è£ ã¯alphaã¢ã¸ã¥ã¼ã«ã使ç¨ãã¦ãã¾ãã
import * as apprunner from '@aws-cdk/aws-apprunner-alpha'; // ä¸ç¥ export class AppRunnerConstruct extends Construct { constructor(scope: Construct, id: string, props: AppRunnerConstructProps) { super(scope, id); // App Runner Serviceãä½æ const appRunnerService = new apprunner.Service(this, 'AppRunnerService', { source: apprunner.Source.fromGitHub({ repositoryUrl: config.repositoryUrl, // ç°å¢ä¾åãã©ã¡ã¼ã¿ã¨ãã¦å¤ã ã branch: 'main', configurationSource: apprunner.ConfigurationSourceType.API, // â Configuration fileã®è¨å®ã«ç¸å½ codeConfigurationValues: { runtime: apprunner.Runtime.PYTHON_3, // â Runtimeã®è¨å®ã«ç¸å½ buildCommand: 'pip install pipenv && pipenv install', // â Build commandã®è¨å®ã«ç¸å½ startCommand: 'pipenv run flask run -h 0.0.0.0 -p 8080', // â Start Commandã®è¨å®ã«ç¸å½ port: '8080', // â Portã®è¨å®ã«ç¸å½ }, connection: apprunner.GitHubConnection.fromConnectionArn(config.connectionARN) // ç°å¢ä¾åãã©ã¡ã¼ã¿ã¨ãã¦å¤ã ã }) })
ã¾ãå ´åã«ãã£ã¦ã¯ç¬èªã®ã«ã¹ã¿ãã¤ãºããããã¨ãããã¾ãã
ä»åã¯cdkã¨apãä¸ã¤ã®Githubãªãã¸ããªã«æ ¼ç´ãããã£ãã®ã§ããã¢ãã¬ãè¨å®ããè¡ãªã£ã¦ããã¾ãã
[アップデート]AWS App Runnerは、ソースディレクトリを指定してコマンド実行できるようになり、モノレポのデプロイに対応しました | DevelopersIO
ãã ãå·çæç¹ã§ã¯Alphaã¢ã¸ã¥ã¼ã«ã®Constructã«ã¢ãã¬ãè¨å®ã®ããã®ããããã£ããªãã£ãã®ã§Escape Hatchesã§è¨å®ãã¦ãã¾ãã
// EscapeHatchesã§ã¢ãã¬ãã®è¨å® const appRunnerCfnService = appRunnerService.node.defaultChild as CfnService; // ãµã³ãã«APãæ ¼ç´ãããã¹ãæå® appRunnerCfnService.addPropertyOverride('SourceConfiguration.CodeRepository.SourceDirectory', '/ap')
æå¾ã«IAMãã¼ã«ã®è¨å®ãè¡ãªã£ã¦ããã¾ãã App RunnerããDynamoDBãæä½ããããã®æ¨©éãå¿ è¦ã«ãªãã¾ãã
WorkShopã®äºåæºåã§æä¾ããã¦ããCloudFormationãã³ãã¬ã¼ãã®ä»¥ä¸ã®é¨åã§ãã
Role: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: ['tasks.apprunner.amazonaws.com'] Action: ['sts:AssumeRole'] Path: / Policies: - PolicyName: ddb-access PolicyDocument: Statement: - Effect: Allow Action: - dynamodb:GetItem - dynamodb:BatchGetItem - dynamodb:Scan - dynamodb:Query - dynamodb:ConditionCheckItem - dynamodb:PutItem - dynamodb:UpdateItem - dynamodb:BatchWriteItem - dynamodb:DeleteItem Resource: - !GetAtt Table.Arn
ä»åã¯ä¸è¨ãåèã«App Runnerã®ãã¼ã«ã«å¯¾ãã¦ç´æ¥æ¨©éãä»ä¸ãã¾ãã
// DynamoDBã¸ã®ã¢ã¯ã»ã¹æ¨©éãä»ä¸ appRunnerService.addToRolePolicy( new iam.PolicyStatement({ actions: [ 'dynamodb:GetItem', 'dynamodb:BatchGetItem', 'dynamodb:Scan', 'dynamodb:Query', 'dynamodb:ConditionCheckItem', 'dynamodb:PutItem', 'dynamodb:UpdateItem', 'dynamodb:BatchWriteItem', 'dynamodb:DeleteItem', ], resources: [props.tableArn], // DynamoDBãã¼ãã«ã®ARNã¯propsã§åãåã£ã¦è¨å® }) )
ããã§ä»åã®ãã³ãºãªã³ã®å®è£ ã¯æ¦ãå®äºã§ãã
ãã¨ã¯cdk deploy
ããã¦åä½ç¢ºèªãè¡ãã¾ãã
ä»åã¯WorkShopã®æé ãè¡ãããµã³ãã«APã®ãã¼ã¸ã表示ããããã¨ã確èªãã¾ããã
åé¡ãªããã°cdk deploy
ã§ãªã½ã¼ã¹ãåé¤ãã¦ãã³ãºãªã³ã¨ãã¦ã¯çµããã§ãã
4. READMEã«ã¢ã¼ããã¯ãã£å³ãWorkShopã®ãªã³ã¯ãã¤ãã¦ããã·ã¥
ãã³ãºãªã³ãçµãã£ããã¾ã¨ãã¨ãã¦ãREADMEã«å¾ã§æ¯ãè¿ãç¨ã®æ å ±ãæ´çãã¦ããã¾ãã
以ä¸ã¤ã¡ã¼ã¸ã§ãã
主ã«æ¸ãã¦ããå 容ã¯ä»¥ä¸ã§ãã
- ã©ã®WorkShopãå®è£ ãããããªã³ã¯ãªã©
- ã¢ã¼ããã¯ãã£å³ãä½ææ¹æ³ã¯å¾è¿°
- ãªãã¸ããªã®æ§æãapãå«ããã¢ãã¬ãæ§æãªã©ãã¦ããå ´åã¯ç¹ã«ã
- ãããã¤æ¹æ³ãäºåã«æåã®ãªã½ã¼ã¹ä½æãå¿
è¦ãcontextå¤ã®æå®ãå¿
è¦ãªã©ãåã«
cdk deploy
ã§ã¯ãããã¤ãè¡ããªãå ´åã¯ç¹ã«ã
ã¢ã¼ããã¯ãã£å³ã¯VSCodeã®Draw.ioæ¡å¼µæ©è½ã§ä½æãã¦ãªãã¸ããªã«æ ¼ç´ãããã¨ãå¤ãã§ãã
ãã®ã¨ãã«ãããã£ã¦ãããã¨ã¯ä»¥ä¸ã§ãã
ã¢ã¼ããã¯ãã£ã®ãã¡ã¤ã«åã¯
ãã¡ã¤ã«å.drawios.svg
ã«ãããããããã¨VSCodeã§éããå ´åã¯Draw.ioæ¡å¼µæ©è½ã§ç·¨éãè¡ããä»ã®ãã¼ã«ããã¯svgãã¡ã¤ã«ã¨ãã¦æ±ãã¾ããREADMEãããã¹æå®ããã°ç»åã¨ãã¦åãè¾¼ããã®ã§ä¾¿å©ã§ããèæ¯ã¯ç½ã«ããããã®ã¾ã¾ã ã¨èæ¯ãééã«ãªãããã¼ã¯ã¢ã¼ãã®å ´åãªã©ã«è¦è¾ããªãã®ã§èæ¯ã¯ç½ã«ãã¾ãã以ä¸ã®é¨åã«ãªãã¾ãã
çµãã£ãããªãã¸ããªã«Pushãä¸é£ã®ä½æ¥å®äºã§ãã
çµããã«
CDKã§ãã³ãºãªã³ãè¡ãããã«ãªã£ã¦ããé ã«æ®ãããããªããã³ã¼ããè³ç£ã¨ãã¦æã¦ããªã©ã¡ãªãããæãã¦ãã¾ãã
ä»ã«ãèªåã¯ãã³ãºãªã³ãããã£ã¦ããï¼ãªã©ãã¯ããã¯ãããã°ãã²æãã¦ãã ããã