- ã¯ããã«
- AWS CDK ã¨ã¯
- ã«ã¹ã¿ã ãªã½ã¼ã¹ã¨ã¯
- ä»åã®ç®ç
- ãã£ã¦ã¿ã
- ã¾ã¨ã
ã¯ããã«
ããã«ã¡ã¯ãã¢ã³ãã³ã¯ã¤ã«ãºã«åãã¦ãµã³ãã¬ã¤ã¯ã§ã¬ã³ã©ã³ã¹ç·´ç¿ä¸ã®ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¹é¨ãã£ããããã¡ã³ããµã¼ãã¹ï¼èª²ã®ååºã§ãã
AWS CDKã使ã£ã¦ããã客æ§ããCloudFormationã§ãµãã¼ãããã¦ããªããªã½ã¼ã¹ãIaCã§ç®¡çãããã¨ããè¦æãããã対象ç®æã®ã¿ã«ã¹ã¿ã ãªã½ã¼ã¹ã§ä½æãããã¨ã«ãªãã¾ããã ãã®éãAWS CDKã¨ã«ã¹ã¿ã ãªã½ã¼ã¹ã«ã¤ãã¦å°ãåå¼·ãã¾ããã®ã§åå¿é²ãå«ãã¦æ¸ããã¦ããã ãã¾ãã
AWS CDK ã¨ã¯
AWS Cloud Development Kitï¼AWS CDKï¼ã¯ãTypeScriptãPythonãJavaãªã©ã®ä¸è¬çãªããã°ã©ãã³ã°è¨èªã使ç¨ãã¦ãAWSãªã½ã¼ã¹ãã³ã¼ãã¨ãã¦å®ç¾©ããAWS CloudFormationãéãã¦ãããã¸ã§ãã³ã°ããããã®ãªã¼ãã³ã½ã¼ã¹ã®ã½ããã¦ã§ã¢éçºãã¬ã¼ã ã¯ã¼ã¯ã§ãã AWS CDKã使ç¨ãããã¨ã§ãéçºè ã¯æ £ã親ããã ããã°ã©ãã³ã°è¨èªã§ã¤ã³ãã©ã¹ãã©ã¯ãã£ãå®ç¾©ã§ããã³ã¼ãã®åå©ç¨æ§ãä¿å®æ§ãåä¸ãã¾ãã ã¾ããAWS CloudFormationã¨ã®é£æºã«ãããã¤ã³ãã©ã¹ãã©ã¯ãã£ã®ãããã¤ã管çãå¹çåããã¾ãã
AWS CDK ã¯ä»¥ä¸ã®ãããªç¹å¾´ãæã£ã¦ãã¾ãã
- ããã°ã©ãã³ã°è¨èªã®ãµãã¼ã
- TypeScriptãJavaScriptãPythonãJavaãC#ãªã©ã®è¨èªããµãã¼ããã¦ãããéçºè ã¯é¦´æã¿ã®ããè¨èªã§ã¤ã³ãã©ã¹ãã©ã¯ãã£ãå®ç¾©ã§ãã¾ãã
- AWS CloudFormationã¨ã®çµ±å
- AWS CloudFormationãéãã¦ãªã½ã¼ã¹ããããã¸ã§ãã³ã°ãããããã¨ã©ã¼æã®ãã¼ã«ããã¯ããããã¤ã®äºæ¸¬å¯è½æ§ãªã©ã®å©ç¹ã享åã§ãã¾ã
ã«ã¹ã¿ã ãªã½ã¼ã¹ã¨ã¯
ã«ã¹ã¿ã ãªã½ã¼ã¹ã¨ã¯ãCloudFormationã®æ©è½ã§ãæ¨æºã®ãªã½ã¼ã¹ã¿ã¤ãã§ã¯å¯¾å¿ã§ããªãç¹å®ã®ãããã¸ã§ãã³ã°ãã¸ãã¯ãæä½ããã¦ã¼ã¶ã¼ãç¬èªã«å®ç¾©ãã¦å®è¡ã§ããæ©è½ã§ããããã«ãããCloudFormationããµãã¼ããã¦ããªããªã½ã¼ã¹ããè¤éãªè¨å®ãå«ããªã½ã¼ã¹ããã³ãã¬ã¼ãå ã§ç®¡çãããã¨ãå¯è½ã«ãªãã¾ãã å ¬å¼ããã¥ã¡ã³ã
AWS CDKã§ã¯ãCustomResource
ã¯ã©ã¹ã使ç¨ãã¦ã«ã¹ã¿ã ãªã½ã¼ã¹ãå®ç¾©ãããã¨ãã§ãã¾ãã
ãã ããã«ã¹ã¿ã ãªã½ã¼ã¹ã®å©ç¨ã¯ã¨ã©ã¼ãã³ããªã³ã°ãçµæã®åºåãªã©ã§ç¬èªã®ãã¸ãã¯ãå«ããããé©åã«è¨è¨ã»å®è£
ããå¿
è¦ãããã¾ãã
ä¸è¬çã«ã«ã¹ã¿ã ãªã½ã¼ã¹ã§ã¯Lambdaé¢æ°ã使ããã¨ãå¤ãã§ãããã¨ã©ã¼ãã³ããªã³ã°ãCloudFormationã¸ã®ã¬ã¹ãã³ã¹ã®å®ç¾©ãªã©æ £ãã¦ããªãã¨å®è£ ãé£ãããã®ã¨ãªã£ã¦ãã¾ãã ä»åã¯Lambdaã使ããã«ãAwsSdkCallã§ã«ã¹ã¿ã ãªã½ã¼ã¹ãä½ã£ã¦ã¿ã¾ããï¼å é¨ã§ã¯Lambdaé¢æ°ãä½æããã¦ãã¾ããï¼
ä»åã®ç®ç
ä»åã¯AWS CDK ã¨ã«ã¹ã¿ã ãªã½ã¼ã¹ããã¥ã¼ããªã¢ã«ã¨ãããã¨ã§ã以ä¸ã®ãããªãã³ãã¬ã¼ããä½æãã¾ãã
- 2ã¤ã®ãã±ãããä½æãã
- 1ã¤ã®ãã±ããã«åæãã¼ã¿ãã¡ã¤ã«ãã¢ãããã¼ãããï¼cdkã«ãµãã¼ããããã«ã¹ã¿ã ãªã½ã¼ã¹ï¼
- 1ã¤ç®ã®ãã±ããããï¼ã¤ç®ã®ãã±ããã«åæãã¼ã¿ãã¡ã¤ã«ãã³ãã¼ããï¼cdkã«ãµãã¼ãããã¦ããªãã«ã¹ã¿ã ãªã½ã¼ã¹ï¼
ãã¥ã¼ããªã¢ã«ã¨ãããã¨ã§ãå®ç¨æ§ã¯ä½ãã§ãããæé ãã³ã¼ãã®æ¸ãæ¹ãªã©ã§åèã«ãªãã°å¹¸ãã§ãã
ãã£ã¦ã¿ã
AWS CDK ã®ã¤ã³ã¹ãã¼ã«
AWS CDK ãã¤ã³ã¹ãã¼ã«ããåã«ãNode.jsãã¤ã³ã¹ãã¼ã«ãã¾ãã
Linuxã®å ´å
# nvm ã®ã¤ã³ã¹ãã¼ã« curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash # nvm ãæå¹å source ~/.bashrc # ã¾ã㯠`source ~/.zshrc` # Node.js ã®ã¤ã³ã¹ãã¼ã« nvm install --lts # ãã¼ã¸ã§ã³ç¢ºèª node -v npm -v
Node.jsãã¤ã³ã¹ãã¼ã«ããããã以ä¸ã®ã³ãã³ãã§AWS CDKãã¤ã³ã¹ãã¼ã«ãã¾ãã
npm install -g aws-cdk
以ä¸ã®ã³ãã³ãã§CDKãæ£ããã¤ã³ã¹ãã¼ã«ãããã確èªãã¾ã
cdk --version
AWS CDK ããã¸ã§ã¯ãã®åæå
以ä¸ã®ã³ãã³ãã§CDK ããã¸ã§ã¯ããåæåãã¾ãã
mkdir cdk-s3-example cd cdk-s3-example cdk init app --language python
åæåã«æåããã¨ä»¥ä¸ã®ãã¡ã¤ã«ãä½æããã¾ã
. âââ README.md âââ app.py âââ cdk.json âââ cdk_s3_example â âââ __init__.py â âââ cdk_s3_example_stack.py âââ requirements-dev.txt âââ requirements.txt âââ source.bat âââ tests âââ __init__.py âââ unit âââ __init__.py âââ test_cdk_s3_example_stack.py
S3ãã±ãããä½æããCDKãä½æãã
cdk_s3_example/cdk_s3_example_stack.py
ã以ä¸ã®ããã«è¨è¿°ãã¾ã
from aws_cdk import ( Stack, aws_s3 as s3, RemovalPolicy, ) from constructs import Construct class CdkS3ExampleStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # ã½ã¼ã¹ãã±ãããä½æ source_bucket = s3.Bucket( self, "SourceBucket", versioned=False, # ãã¼ã¸ã§ãã³ã°ãç¡å¹å removal_policy=RemovalPolicy.DESTROY, # ã¹ã¿ãã¯åé¤æã«ãã±ãããåé¤ auto_delete_objects=True, # ãã±ããã®ä¸èº«ãåé¤ ) # ãã¹ãã£ãã¼ã·ã§ã³ãã±ãããä½æ destination_bucket = s3.Bucket( self, "DestinationBucket", versioned=False, # ãã¼ã¸ã§ãã³ã°ãç¡å¹å removal_policy=RemovalPolicy.DESTROY, # ã¹ã¿ãã¯åé¤æã«ãã±ãããåé¤ auto_delete_objects=True, # ãã±ããã®ä¸èº«ãåé¤ )
ä»ã®ãã¡ã¤ã«ã¯å¤æ´ããã«ä»¥ä¸ã®ã³ãã³ããå®è¡ãã¾ã
cdk synth
ãã®ã³ãã³ãã§CloudFormationãã³ãã¬ã¼ããä½æããã¾ã
次ã«ä»¥ä¸ã®ã³ãã³ããå®è¡ãã¾ã
cdk deploy
ãã®ã³ãã³ãã§CloudFormationã¹ã¿ãã¯ãä½æããã¾ãã
ï¼ã¤ã®S3ãã±ãããä½æããããã¨ã確èªãã¦ãã ããã
åæãã¼ã¿ãã¡ã¤ã«ãã¢ãããã¼ããã
ããã¸ã§ã¯ãã®ã«ã¼ããã£ã¬ã¯ããªã«initial-data/
ãã©ã«ããä½æããcdk-s3-example/initial-data/file.txt
ãã¡ã¤ã«ãä½æãã¾ãã
ãã®ãã¡ã¤ã«ãsource_bucketã«è¿½å ãã¾ã
aws_cdk.aws_s3_deployment
ã使ç¨ãããã¨ã§ãä½æããSï¼ãã±ããã«ãã¡ã¤ã«ãã¢ãããã¼ãããå¦çã追å ãããã¨ãã§ãã¾ãã
ãã®ã¢ã¸ã¥ã¼ã«ãã«ã¹ã¿ã ãªã½ã¼ã¹ã使ç¨ãã¦ãããå é¨ã§ã¯Lambdaé¢æ°ãä½æãã¦ãã¾ãã CDKã§ã¯Lambdaé¢æ°ãä½æããã«ç°¡æã«ã«ã¹ã¿ã ãªã½ã¼ã¹ãä½æãããã¨ãã§ãã¾ãã
cdk-s3-example/cdk_s3_example/cdk_s3_example_stack.py
ã以ä¸ã®ããã«è¨è¿°ãã¾ã
from aws_cdk import ( Stack, aws_s3 as s3, aws_s3_deployment as s3_deployment, RemovalPolicy, ) from constructs import Construct class CdkS3ExampleStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # ã½ã¼ã¹ãã±ãããä½æ source_bucket = s3.Bucket( self, "SourceBucket", versioned=False, # ãã¼ã¸ã§ãã³ã°ãç¡å¹å removal_policy=RemovalPolicy.DESTROY, # ã¹ã¿ãã¯åé¤æã«ãã±ãããåé¤ auto_delete_objects=True, # ãã±ããã®ä¸èº«ãåé¤ ) # ãã¹ãã£ãã¼ã·ã§ã³ãã±ãããä½æ destination_bucket = s3.Bucket( self, "DestinationBucket", versioned=False, # ãã¼ã¸ã§ãã³ã°ãç¡å¹å removal_policy=RemovalPolicy.DESTROY, # ã¹ã¿ãã¯åé¤æã«ãã±ãããåé¤ auto_delete_objects=True, # ãã±ããã®ä¸èº«ãåé¤ ) # åæãã¼ã¿ãã¢ãããã¼ã initial_data_deployment = s3_deployment.BucketDeployment( self, "DeployInitialData", sources=[ s3_deployment.Source.asset("initial-data") ], # initial-dataãã£ã¬ã¯ããªå ã®ãã¡ã¤ã« destination_bucket=source_bucket, ) # æ示çãªä¾åé¢ä¿ã追å initial_data_deployment.node.add_dependency(source_bucket)
ãã±ããä½æå¾ã«ãã¼ã¿ã¢ãããã¼ãå¦çãè¡ãå¿ è¦ããããããæ示çãªä¾åé¢ä¿ã追å ãã¦ãã¾ãã
S3_deployment.BucketDeploymentã®å ¬å¼ããã¥ã¡ã³ã
S3ãã±ããå ã®ãã¡ã¤ã«ãã³ãã¼
aws_cdk.Custom_resource.AwsCustomResource
ã¨aws_cdk.Custom_resource.AwsSdkCall
ã使ç¨ãããã¨ã§ãAWS SDKã§å¯è½ãªæä½ãLambdaé¢æ°ãä½æããã«ã«ã¹ã¿ã ãªã½ã¼ã¹ã§å®ç¾©ãããã¨ãã§ãã¾ãã
ä»åã¯CopyObjectããã¦ãã¾ãããå¹
åºãå¿ç¨ã§ãããã§ãã
AwsCustomResourceã®å ¬å¼ããã¥ã¡ã³ã
AwsSdkCallã®å ¬å¼ããã¥ã¡ã³ã
AwsCustomResource
ã§ã¯å®è¡ãã¼ã«ãå®ç¾©ããå¿
è¦ãããã®ã§å¿ããªãããã«ãã¦ãã ããã
ãã¼ã¿ã¢ãããã¼ãå¦çã®å¾ã«è¡ãå¿
è¦ããããããæ示çãªä¾åé¢ä¿ã追å ãã¦ãã¾ãã
cdk-s3-example/cdk_s3_example/cdk_s3_example_stack.py
ã以ä¸ã®ããã«è¨è¿°ãã¾ã
from aws_cdk import ( Stack, aws_s3 as s3, aws_s3_deployment as s3_deployment, RemovalPolicy, aws_iam as iam, custom_resources as cr, Duration, ) from constructs import Construct class CdkS3ExampleStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # ã½ã¼ã¹ãã±ãããä½æ source_bucket = s3.Bucket( self, "SourceBucket", versioned=False, # ãã¼ã¸ã§ãã³ã°ãç¡å¹å removal_policy=RemovalPolicy.DESTROY, # ã¹ã¿ãã¯åé¤æã«ãã±ãããåé¤ auto_delete_objects=True, # ãã±ããã®ä¸èº«ãåé¤ ) # ãã¹ãã£ãã¼ã·ã§ã³ãã±ãããä½æ destination_bucket = s3.Bucket( self, "DestinationBucket", versioned=False, # ãã¼ã¸ã§ãã³ã°ãç¡å¹å removal_policy=RemovalPolicy.DESTROY, # ã¹ã¿ãã¯åé¤æã«ãã±ãããåé¤ auto_delete_objects=True, # ãã±ããã®ä¸èº«ãåé¤ ) # åæãã¼ã¿ãã¢ãããã¼ã initial_data_deployment = s3_deployment.BucketDeployment( self, "DeployInitialData", sources=[ s3_deployment.Source.asset("initial-data") ], # initial-dataãã£ã¬ã¯ããªå ã®ãã¡ã¤ã« destination_bucket=source_bucket, ) # ã«ã¹ã¿ã ãªã½ã¼ã¹ã®IAMãã¼ã«ãä½æ custom_resource_role = iam.Role( self, "CustomResourceRole", assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"), ) # IAMãã¼ã«ã«S3ã®æ¨©éãä»ä¸ custom_resource_role.add_to_policy( iam.PolicyStatement( actions=[ "s3:ListBucket", # ãã±ããã®ãªã¹ãæä½ "s3:GetObject", # ãªãã¸ã§ã¯ãã®èªã¿åã "s3:CopyObject", # ãªãã¸ã§ã¯ãã®ã³ãã¼ "s3:PutObject", # ãªãã¸ã§ã¯ãã®æ¸ã込㿠], resources=[ source_bucket.bucket_arn, # ã½ã¼ã¹ãã±ããã¸ã®æä½ f"{source_bucket.bucket_arn}/*", # ã½ã¼ã¹ãã±ããå ã®ãã¹ã¦ã®ãªãã¸ã§ã¯ã destination_bucket.bucket_arn, # ãã¹ãã£ãã¼ã·ã§ã³ãã±ããã¸ã®æä½ f"{destination_bucket.bucket_arn}/*", # ãã¹ãã£ãã¼ã·ã§ã³ãã±ããå ã®ãã¹ã¦ã®ãªãã¸ã§ã¯ã ], ) ) # ã½ã¼ã¹ãã±ãããããã¹ãã£ãã¼ã·ã§ã³ãã±ããã¸ã®ã³ãã¼ custom_provider = cr.AwsCustomResource( self, "CopyObjects", on_create=cr.AwsSdkCall( action="copyObject", service="S3", parameters={ "Bucket": destination_bucket.bucket_name, "CopySource": f"{source_bucket.bucket_name}/file.txt", "Key": "file.txt", }, physical_resource_id=cr.PhysicalResourceId.of("CopyObjects"), ), role=custom_resource_role, timeout=Duration.seconds(30), ) # æ示çãªä¾åé¢ä¿ã追å initial_data_deployment.node.add_dependency(source_bucket) custom_provider.node.add_dependency(initial_data_deployment)
å®è£ å®äº
ããã¾ã§ã§ããããcdk synth
ã¨cdk deploy
ãå®è¡ããã¨ãï¼ã¤ã®ãã±ããã«ãã¡ã¤ã«ããããã¨ã確èªã§ããã¨æãã¾ãã
ã¾ã¨ã
ä»åã¯ãAWS CDKã¨ã«ã¹ã¿ã ãªã½ã¼ã¹ã使ã£ã¦ã¿ã¾ãããCloudFormationã§ãµãã¼ãããã¦ããªããªã½ã¼ã¹ãIaCã§ç®¡çãããå ´åãªã©ã§ã«ã¹ã¿ã ãªã½ã¼ã¹ã使ãå¿ è¦ãããã±ã¼ã¹ããããã¨æãã¾ãã®ã§åèã«ãªãã°å¹¸ãã§ãã ã¡ãªã¿ã«ãCloudFormationã§ãµãã¼ãããã¦ããªãã¦ããTerraformã§ã¯ãµãã¼ãããã¦ãããªã½ã¼ã¹ãããã¾ããï¼AWS StorageGateway ãªã©ï¼ ã«ã¹ã¿ã ãªã½ã¼ã¹ã¯ã¨ã©ã¼ãã³ããªã³ã°ãä½æãæ´æ°ãåé¤ãã¨ã«æåãå®ç¾©ããã¨ãã£ãæéããããã¾ãã®ã§ã使ç¨ã®éã¯ãã£ããã¨æ¤è¨ãããã¨ããå§ããã¾ãã
ååº å®ç´(å·çè¨äºã®ä¸è¦§)
ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¹é¨ãã£ããããã¡ã³ããµã¼ãã¹1課
2024å¹´9æä¸éå ¥ç¤¾ã§ãã æ¯æï¼æéè³æ ¼åå¼·ç¶ç¶ä¸ã§ãã