aws cdkをそろそろ本気で覚えたくなったのでとりあえず使ってみた。
やること
aws cdkでLambda(Node.js20)とS3バケットを作成。S3バケット内にtest.txtを配置する。
前提
aws cdkの利用準備ができていること
※まだの場合、下記を参考に手順2まで実施する。
amegaeru.hatenablog.jp
実践!
1.cdk プロジェクト作成
1-1.PowerShellを開き下記を実行
> mkdir cdk-s3-lambda
> cd cdk-s3-lambda
> cdk init app --language typescript
2.コード作成
2-1.lib/cdk-s3-lambda-stack.tsを開き下記を入力
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import * as path from 'path';
export class CdkS3LambdaStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// S3バケットの作成
const bucket = new s3.Bucket(this, 'MyBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});
// アセットディレクトリの作成
const asset = s3deploy.Source.asset(path.join(__dirname, 'assets'));
// S3バケットにファイルを配置
new s3deploy.BucketDeployment(this, 'DeployTestTxt', {
sources: [asset],
destinationBucket: bucket,
});
// Lambda関数の作成
const lambdaFunction = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_20_X, // 修正箇所
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda')),
environment: {
BUCKET_NAME: bucket.bucketName,
},
});
// Lambda関数にS3バケットへのアクセス権を付与
bucket.grantRead(lambdaFunction);
// Lambda関数のIAMロールにS3アクセス権限を追加
lambdaFunction.addToRolePolicy(new iam.PolicyStatement({
actions: ['s3:GetObject'],
resources: [`${bucket.bucketArn}/*`],
}));
}
}
3.Lambda関数作成
3-1.lib/lambdaフォルダを作成
3-2.lib/lambdaフォルダ内にindex.jsファイルを作成
3-3.index.jsファイルを開き下記を入力
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
const bucketName = process.env.BUCKET_NAME;
const key = 'test.txt';
try {
const data = await s3.getObject({ Bucket: bucketName, Key: key }).promise();
const fileContent = data.Body.toString('utf-8');
console.log('File Content:', fileContent);
} catch (error) {
console.error('Error getting object from S3:', error);
}
};
4.S3バケット内にindex.txtファイルを作成
4-1.lib/assetsフォルダを作成
4-2.lib/assetsフォルダー内にindex.txtファイルを作成
5.cdkデプロイ
5-1.PowerShellで下記を実行
> cdk bootstrap --profile <profile名>
PS C:\cdk-s3-lambda> cdk bootstrap --profile testvault
⏳ Bootstrapping environment aws://xxxxxxxxxxxx/ap-northeast-1...
Trusted accounts for deployment: (none)
Trusted accounts for lookup: (none)
Using default execution policy of 'arn:aws:iam::aws:policy/AdministratorAccess'. Pass '--cloudformation-execution-policies' to customize.
CDKToolkit: creating CloudFormation changeset...
CDKToolkit | 0/4 | 20:27:20 | UPDATE_IN_PROGRESS | AWS::CloudFormation::Stack | CDKToolkit User Initiated
CDKToolkit | 0/4 | 20:27:24 | UPDATE_IN_PROGRESS | AWS::SSM::Parameter | CdkBootstrapVersion
CDKToolkit | 1/4 | 20:27:25 | UPDATE_COMPLETE | AWS::SSM::Parameter | CdkBootstrapVersion
CDKToolkit | 1/4 | 20:27:27 | UPDATE_IN_PROGRESS | AWS::IAM::Role | DeploymentActionRole
CDKToolkit | 2/4 | 20:27:44 | UPDATE_COMPLETE | AWS::IAM::Role | DeploymentActionRole
CDKToolkit | 3/4 | 20:27:45 | UPDATE_COMPLETE_CLEA | AWS::CloudFormation::Stack | CDKToolkit
CDKToolkit | 4/4 | 20:27:47 | UPDATE_COMPLETE | AWS::CloudFormation::Stack | CDKToolkit
✅ Environment aws://xxxxxxxxxxxx/ap-northeast-1 bootstrapped.
5-2.PowerShellで下記を実行
> cdk deploy --profile <profile名>
PS C:\cdk-s3-lambda> cdk deploy --profile testvault
✨ Synthesis time: 6.31s
CdkS3LambdaStack: start: Building 3322b7049fb0ed2b7cbb644a2ada8d1116ff80c32dca89e6ada846b5de26f961:current_account-current_region
CdkS3LambdaStack: success: Built 3322b7049fb0ed2b7cbb644a2ada8d1116ff80c32dca89e6ada846b5de26f961:current_account-current_region
CdkS3LambdaStack: start: Building 2d56e153cac88d3e0c2f842e8e6f6783b8725bf91f95e0673b4725448a56e96d:current_account-current_region
CdkS3LambdaStack: success: Built 2d56e153cac88d3e0c2f842e8e6f6783b8725bf91f95e0673b4725448a56e96d:current_account-current_region
CdkS3LambdaStack: start: Publishing 3322b7049fb0ed2b7cbb644a2ada8d1116ff80c32dca89e6ada846b5de26f961:current_account-current_region
CdkS3LambdaStack: start: Building 0a92ae4f0ff7188d013fc02ca4812b731af9e914f9ebaeffe18cb1c818b50d28:current_account-current_region
CdkS3LambdaStack: success: Built 0a92ae4f0ff7188d013fc02ca4812b731af9e914f9ebaeffe18cb1c818b50d28:current_account-current_region
CdkS3LambdaStack: start: Publishing 2d56e153cac88d3e0c2f842e8e6f6783b8725bf91f95e0673b4725448a56e96d:current_account-current_region
CdkS3LambdaStack: start: Publishing 0a92ae4f0ff7188d013fc02ca4812b731af9e914f9ebaeffe18cb1c818b50d28:current_account-current_region
CdkS3LambdaStack: start: Building d863e43b5651fd751e08d2380e4a998a67ca0774fffc4c236f6f2dee371d1419:current_account-current_region
CdkS3LambdaStack: success: Built d863e43b5651fd751e08d2380e4a998a67ca0774fffc4c236f6f2dee371d1419:current_account-current_region
CdkS3LambdaStack: start: Publishing d863e43b5651fd751e08d2380e4a998a67ca0774fffc4c236f6f2dee371d1419:current_account-current_region
CdkS3LambdaStack: start: Building 8203404b07eea758f452b7933e4f308adcba25d7ef3330871a46c2c10bdb895c:current_account-current_region
CdkS3LambdaStack: success: Built 8203404b07eea758f452b7933e4f308adcba25d7ef3330871a46c2c10bdb895c:current_account-current_region
CdkS3LambdaStack: start: Publishing 8203404b07eea758f452b7933e4f308adcba25d7ef3330871a46c2c10bdb895c:current_account-current_region
CdkS3LambdaStack: success: Published d863e43b5651fd751e08d2380e4a998a67ca0774fffc4c236f6f2dee371d1419:current_account-current_region
CdkS3LambdaStack: success: Published 2d56e153cac88d3e0c2f842e8e6f6783b8725bf91f95e0673b4725448a56e96d:current_account-current_region
CdkS3LambdaStack: success: Published 0a92ae4f0ff7188d013fc02ca4812b731af9e914f9ebaeffe18cb1c818b50d28:current_account-current_region
CdkS3LambdaStack: success: Published 8203404b07eea758f452b7933e4f308adcba25d7ef3330871a46c2c10bdb895c:current_account-current_region
CdkS3LambdaStack: success: Published 3322b7049fb0ed2b7cbb644a2ada8d1116ff80c32dca89e6ada846b5de26f961:current_account-current_region
This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:
IAM Statement Changes
┌───┬───────────────────────────────┬────────┬───────────────────────────────┬────────────────────────────────┬───────────┐
│ │ Resource │ Effect │ Action │ Principal │ Condition │
├───┼───────────────────────────────┼────────┼───────────────────────────────┼────────────────────────────────┼───────────┤
│ + │ ${Custom::CDKBucketDeployment │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.com │ │
│ │ 8693BB64968944B69AAFB0CC9EB87 │ │ │ │ │
│ │ 56C/ServiceRole.Arn} │ │ │ │ │
├───┼───────────────────────────────┼────────┼───────────────────────────────┼────────────────────────────────┼───────────┤
│ + │ ${Custom::S3AutoDeleteObjects │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.com │ │
│ │ CustomResourceProvider/Role.A │ │ │ │ │
│ │ rn} │ │ │ │ │
├───┼───────────────────────────────┼────────┼───────────────────────────────┼────────────────────────────────┼───────────┤
│ + │ ${MyBucket.Arn} │ Allow │ s3:DeleteObject* │ AWS:${Custom::S3AutoDeleteObje │ │
│ │ ${MyBucket.Arn}/* │ │ s3:GetBucket* │ ctsCustomResourceProvider/Role │ │
│ │ │ │ s3:List* │ .Arn} │ │
│ │ │ │ s3:PutBucketPolicy │ │ │
│ + │ ${MyBucket.Arn} │ Allow │ s3:Abort* │ AWS:${Custom::CDKBucketDeploym │ │
│ │ ${MyBucket.Arn}/* │ │ s3:DeleteObject* │ ent8693BB64968944B69AAFB0CC9EB │ │
│ │ │ │ s3:GetBucket* │ 8756C/ServiceRole} │ │
│ │ │ │ s3:GetObject* │ │ │
│ │ │ │ s3:List* │ │ │
│ │ │ │ s3:PutObject │ │ │
│ │ │ │ s3:PutObjectLegalHold │ │ │
│ │ │ │ s3:PutObjectRetention │ │ │
│ │ │ │ s3:PutObjectTagging │ │ │
│ │ │ │ s3:PutObjectVersionTagging │ │ │
│ + │ ${MyBucket.Arn} │ Allow │ s3:GetBucket* │ AWS:${MyFunction/ServiceRole} │ │
│ │ ${MyBucket.Arn}/* │ │ s3:GetObject* │ │ │
│ │ │ │ s3:List* │ │ │
├───┼───────────────────────────────┼────────┼───────────────────────────────┼────────────────────────────────┼───────────┤
│ + │ ${MyBucket.Arn}/* │ Allow │ s3:GetObject │ AWS:${MyFunction/ServiceRole} │ │
├───┼───────────────────────────────┼────────┼───────────────────────────────┼────────────────────────────────┼───────────┤
│ + │ ${MyFunction/ServiceRole.Arn} │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.com │ │
├───┼───────────────────────────────┼────────┼───────────────────────────────┼────────────────────────────────┼───────────┤
│ + │ arn:${AWS::Partition}:s3:::{" │ Allow │ s3:GetBucket* │ AWS:${Custom::CDKBucketDeploym │ │
│ │ Fn::Sub":"cdk-hnb659fds-asset │ │ s3:GetObject* │ ent8693BB64968944B69AAFB0CC9EB │ │
│ │ s-${AWS::AccountId}-${AWS::Re │ │ s3:List* │ 8756C/ServiceRole} │ │
│ │ gion}"} │ │ │ │ │
│ │ arn:${AWS::Partition}:s3:::{" │ │ │ │ │
│ │ Fn::Sub":"cdk-hnb659fds-asset │ │ │ │ │
│ │ s-${AWS::AccountId}-${AWS::Re │ │ │ │ │
│ │ gion}"}/* │ │ │ │ │
└───┴───────────────────────────────┴────────┴───────────────────────────────┴────────────────────────────────┴───────────┘
IAM Policy Changes
┌───┬──────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────┐
│ │ Resource │ Managed Policy ARN │
├───┼──────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│ + │ ${Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLa │
│ │ 8756C/ServiceRole} │ mbdaBasicExecutionRole │
├───┼──────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│ + │ ${Custom::S3AutoDeleteObjectsCustomResourceProvider/Role │ {"Fn::Sub":"arn:${AWS::Partition}:iam::aws:policy/servic │
│ │ } │ e-role/AWSLambdaBasicExecutionRole"} │
├───┼──────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│ + │ ${MyFunction/ServiceRole} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLa │
│ │ │ mbdaBasicExecutionRole │
└───┴──────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)
Do you wish to deploy these changes (y/n)? y
CdkS3LambdaStack: start: Building d7c71b0feef6812e8923395620bb28e50c0d5413572c070f5fc30f549a5ef3ec:current_account-current_region
CdkS3LambdaStack: success: Built d7c71b0feef6812e8923395620bb28e50c0d5413572c070f5fc30f549a5ef3ec:current_account-current_region
CdkS3LambdaStack: start: Publishing d7c71b0feef6812e8923395620bb28e50c0d5413572c070f5fc30f549a5ef3ec:current_account-current_region
CdkS3LambdaStack: success: Published d7c71b0feef6812e8923395620bb28e50c0d5413572c070f5fc30f549a5ef3ec:current_account-current_region
CdkS3LambdaStack: deploying... [1/1]
CdkS3LambdaStack: creating CloudFormation changeset...
CdkS3LambdaStack | 0/15 | 20:34:06 | REVIEW_IN_PROGRESS | AWS::CloudFormation::Stack | CdkS3LambdaStack User Initiated
CdkS3LambdaStack | 0/15 | 20:34:13 | CREATE_IN_PROGRESS | AWS::CloudFormation::Stack | CdkS3LambdaStack User Initiated
CdkS3LambdaStack | 0/15 | 20:34:17 | CREATE_IN_PROGRESS | AWS::IAM::Role | MyFunction/ServiceRole (MyFunctionServiceRole3C357FF2)
CdkS3LambdaStack | 0/15 | 20:34:17 | CREATE_IN_PROGRESS | AWS::IAM::Role | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRole89A01265)
CdkS3LambdaStack | 0/15 | 20:34:17 | CREATE_IN_PROGRESS | AWS::S3::Bucket | MyBucket (MyBucketF68F3FF0)
CdkS3LambdaStack | 0/15 | 20:34:17 | CREATE_IN_PROGRESS | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata)
CdkS3LambdaStack | 0/15 | 20:34:17 | CREATE_IN_PROGRESS | AWS::Lambda::LayerVersion | DeployTestTxt/AwsCliLayer (DeployTestTxtAwsCliLayerEBD84BF7)
CdkS3LambdaStack | 0/15 | 20:34:17 | CREATE_IN_PROGRESS | AWS::IAM::Role | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092)
CdkS3LambdaStack | 0/15 | 20:34:18 | CREATE_IN_PROGRESS | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata)
Resource creation Initiated
CdkS3LambdaStack | 0/15 | 20:34:18 | CREATE_IN_PROGRESS | AWS::S3::Bucket | MyBucket (MyBucketF68F3FF0) Resource creation Initiated
CdkS3LambdaStack | 0/15 | 20:34:18 | CREATE_IN_PROGRESS | AWS::IAM::Role | MyFunction/ServiceRole (MyFunctionServiceRole3C357FF2) Resource creation Initiated
CdkS3LambdaStack | 0/15 | 20:34:18 | CREATE_IN_PROGRESS | AWS::IAM::Role | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRole89A01265) Resource creation Initiated
CdkS3LambdaStack | 0/15 | 20:34:18 | CREATE_IN_PROGRESS | AWS::IAM::Role | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092) Resource creation Initiated
CdkS3LambdaStack | 1/15 | 20:34:19 | CREATE_COMPLETE | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata)
CdkS3LambdaStack | 1/15 | 20:34:24 | CREATE_IN_PROGRESS | AWS::Lambda::LayerVersion | DeployTestTxt/AwsCliLayer (DeployTestTxtAwsCliLayerEBD84BF7) Resource creation Initiated
CdkS3LambdaStack | 2/15 | 20:34:24 | CREATE_COMPLETE | AWS::Lambda::LayerVersion | DeployTestTxt/AwsCliLayer (DeployTestTxtAwsCliLayerEBD84BF7)
CdkS3LambdaStack | 3/15 | 20:34:32 | CREATE_COMPLETE | AWS::S3::Bucket | MyBucket (MyBucketF68F3FF0)
CdkS3LambdaStack | 4/15 | 20:34:35 | CREATE_COMPLETE | AWS::IAM::Role | MyFunction/ServiceRole (MyFunctionServiceRole3C357FF2)
CdkS3LambdaStack | 5/15 | 20:34:35 | CREATE_COMPLETE | AWS::IAM::Role | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRole89A01265)
CdkS3LambdaStack | 6/15 | 20:34:35 | CREATE_COMPLETE | AWS::IAM::Role | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092)
CdkS3LambdaStack | 6/15 | 20:34:36 | CREATE_IN_PROGRESS | AWS::IAM::Policy | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole/DefaultPolicy (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRoleDefaultPolicy88902FDF)
CdkS3LambdaStack | 6/15 | 20:34:36 | CREATE_IN_PROGRESS | AWS::IAM::Policy | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4)
CdkS3LambdaStack | 6/15 | 20:34:36 | CREATE_IN_PROGRESS | AWS::Lambda::Function | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
CdkS3LambdaStack | 6/15 | 20:34:37 | CREATE_IN_PROGRESS | AWS::S3::BucketPolicy | MyBucket/Policy (MyBucketPolicyE7FBAC7B)
CdkS3LambdaStack | 6/15 | 20:34:37 | CREATE_IN_PROGRESS | AWS::Lambda::Function | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F) Resource creation Initiated
CdkS3LambdaStack | 6/15 | 20:34:38 | CREATE_IN_PROGRESS | AWS::IAM::Policy | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole/DefaultPolicy (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRoleDefaultPolicy88902FDF) Resource creation Initiated
CdkS3LambdaStack | 6/15 | 20:34:38 | CREATE_IN_PROGRESS | AWS::IAM::Policy | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4) Resource creation Initiated
CdkS3LambdaStack | 6/15 | 20:34:38 | CREATE_IN_PROGRESS | AWS::S3::BucketPolicy | MyBucket/Policy (MyBucketPolicyE7FBAC7B) Resource creation Initiated
CdkS3LambdaStack | 6/15 | 20:34:38 | CREATE_IN_PROGRESS | AWS::Lambda::Function | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F) Eventual consistency check initiated
CdkS3LambdaStack | 7/15 | 20:34:39 | CREATE_COMPLETE | AWS::S3::BucketPolicy | MyBucket/Policy (MyBucketPolicyE7FBAC7B)
CdkS3LambdaStack | 8/15 | 20:34:43 | CREATE_COMPLETE | AWS::Lambda::Function | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
CdkS3LambdaStack | 8/15 | 20:34:44 | CREATE_IN_PROGRESS | Custom::S3AutoDeleteObjects | MyBucket/AutoDeleteObjectsCustomResource/Default (MyBucketAutoDeleteObjectsCustomResource2C28D565)
CdkS3LambdaStack | 8/15 | 20:34:46 | CREATE_IN_PROGRESS | Custom::S3AutoDeleteObjects | MyBucket/AutoDeleteObjectsCustomResource/Default (MyBucketAutoDeleteObjectsCustomResource2C28D565) Resource creation Initiated
CdkS3LambdaStack | 9/15 | 20:34:46 | CREATE_COMPLETE | Custom::S3AutoDeleteObjects | MyBucket/AutoDeleteObjectsCustomResource/Default (MyBucketAutoDeleteObjectsCustomResource2C28D565)
CdkS3LambdaStack | 10/15 | 20:34:53 | CREATE_COMPLETE | AWS::IAM::Policy | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole/DefaultPolicy (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRoleDefaultPolicy88902FDF)
CdkS3LambdaStack | 11/15 | 20:34:53 | CREATE_COMPLETE | AWS::IAM::Policy | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4)
CdkS3LambdaStack | 11/15 | 20:34:55 | CREATE_IN_PROGRESS | AWS::Lambda::Function | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C81C01536)
CdkS3LambdaStack | 11/15 | 20:34:55 | CREATE_IN_PROGRESS | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1)
CdkS3LambdaStack | 11/15 | 20:34:56 | CREATE_IN_PROGRESS | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1) Resource creation Initiated
CdkS3LambdaStack | 11/15 | 20:34:57 | CREATE_IN_PROGRESS | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1) Eventual consistency check initiated
CdkS3LambdaStack | 11/15 | 20:35:00 | CREATE_IN_PROGRESS | AWS::Lambda::Function | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C81C01536) Resource creation Initiated
CdkS3LambdaStack | 11/15 | 20:35:01 | CREATE_IN_PROGRESS | AWS::Lambda::Function | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C81C01536) Eventual consistency check initiated
CdkS3LambdaStack | 12/15 | 20:35:02 | CREATE_COMPLETE | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1)
CdkS3LambdaStack | 13/15 | 20:35:06 | CREATE_COMPLETE | AWS::Lambda::Function | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C81C01536)
CdkS3LambdaStack | 13/15 | 20:35:07 | CREATE_IN_PROGRESS | Custom::CDKBucketDeployment | DeployTestTxt/CustomResource/Default (DeployTestTxtCustomResource5080A8AF)
13/15 Currently in progress: CdkS3LambdaStack, DeployTestTxtCustomResource5080A8AF
CdkS3LambdaStack | 13/15 | 20:35:53 | CREATE_IN_PROGRESS | Custom::CDKBucketDeployment | DeployTestTxt/CustomResource/Default (DeployTestTxtCustomResource5080A8AF) Resource creation Initiated
CdkS3LambdaStack | 14/15 | 20:35:53 | CREATE_COMPLETE | Custom::CDKBucketDeployment | DeployTestTxt/CustomResource/Default (DeployTestTxtCustomResource5080A8AF)
CdkS3LambdaStack | 15/15 | 20:35:54 | CREATE_COMPLETE | AWS::CloudFormation::Stack | CdkS3LambdaStack
✅ CdkS3LambdaStack
✨ Deployment time: 116.88s
Stack ARN:
arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxx:stack/CdkS3LambdaStack/786f4ca0-48e7-11ef-afc8-0eebc5a3269d
✨ Total time: 122.59s
6.作成確認
6-1.PowerShellで下記を実行
> aws s3 ls --profile <profile名>
PS C:\cdk-s3-lambda> aws s3 ls --profile testvault
2024-05-19 13:44:10 cdk-hnb659fds-assets-xxxxxxxxxxxx-ap-northeast-1
6-2.PowerShellで下記を実行
> aws lambda list-functions --profile <profile名>
PS C:\cdk-s3-lambda> aws lambda list-functions --profile testvault
{
"Functions": [
{
"FunctionName": "CdkS3LambdaStack-MyFunction3BAA72D1-V4UWolFI00KO",
・・・・
7.お掃除
7-1.Powershellで下記を実行
> cdk destroy --profile <Profile名>
PS C:\cdk-s3-lambda> cdk destroy --profile testvault
Are you sure you want to delete: CdkS3LambdaStack (y/n)? y
CdkS3LambdaStack: destroying... [1/1]
CdkS3LambdaStack | 0 | 21:07:39 | DELETE_IN_PROGRESS | AWS::Lambda::Function | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C81C01536)
CdkS3LambdaStack | 1 | 21:07:40 | DELETE_COMPLETE | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1)
CdkS3LambdaStack | 1 | 21:07:34 | DELETE_IN_PROGRESS | AWS::CloudFormation::Stack | CdkS3LambdaStack User Initiated
CdkS3LambdaStack | 1 | 21:07:37 | DELETE_IN_PROGRESS | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata)
CdkS3LambdaStack | 1 | 21:07:37 | DELETE_IN_PROGRESS | Custom::S3AutoDeleteObjects | MyBucket/AutoDeleteObjectsCustomResource/Default (MyBucketAutoDeleteObjectsCustomResource2C28D565)
CdkS3LambdaStack | 1 | 21:07:37 | DELETE_IN_PROGRESS | Custom::CDKBucketDeployment | DeployTestTxt/CustomResource/Default (DeployTestTxtCustomResource5080A8AF)
CdkS3LambdaStack | 1 | 21:07:37 | DELETE_IN_PROGRESS | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1)
CdkS3LambdaStack | 2 | 21:07:38 | DELETE_COMPLETE | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata)
CdkS3LambdaStack | 3 | 21:07:39 | DELETE_COMPLETE | Custom::CDKBucketDeployment | DeployTestTxt/CustomResource/Default (DeployTestTxtCustomResource5080A8AF)
CdkS3LambdaStack | 3 | 21:07:41 | DELETE_IN_PROGRESS | AWS::IAM::Policy | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4)
CdkS3LambdaStack | 4 | 21:07:41 | DELETE_COMPLETE | Custom::S3AutoDeleteObjects | MyBucket/AutoDeleteObjectsCustomResource/Default (MyBucketAutoDeleteObjectsCustomResource2C28D565)
CdkS3LambdaStack | 4 | 21:07:41 | DELETE_IN_PROGRESS | AWS::S3::BucketPolicy | MyBucket/Policy (MyBucketPolicyE7FBAC7B)
CdkS3LambdaStack | 4 | 21:07:41 | DELETE_IN_PROGRESS | AWS::Lambda::Function | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
CdkS3LambdaStack | 5 | 21:07:42 | DELETE_COMPLETE | AWS::IAM::Policy | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4)
CdkS3LambdaStack | 5 | 21:07:42 | DELETE_IN_PROGRESS | AWS::IAM::Role | MyFunction/ServiceRole (MyFunctionServiceRole3C357FF2)
CdkS3LambdaStack | 6 | 21:07:43 | DELETE_COMPLETE | AWS::S3::BucketPolicy | MyBucket/Policy (MyBucketPolicyE7FBAC7B)
CdkS3LambdaStack | 7 | 21:07:43 | DELETE_COMPLETE | AWS::Lambda::Function | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C81C01536)
CdkS3LambdaStack | 7 | 21:07:43 | DELETE_IN_PROGRESS | AWS::IAM::Policy | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole/DefaultPolicy (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRoleDefaultPolicy88902FDF)
CdkS3LambdaStack | 7 | 21:07:43 | DELETE_IN_PROGRESS | AWS::Lambda::LayerVersion | DeployTestTxt/AwsCliLayer (DeployTestTxtAwsCliLayerEBD84BF7)
CdkS3LambdaStack | 8 | 21:07:44 | DELETE_COMPLETE | AWS::IAM::Policy | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole/DefaultPolicy (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRoleDefaultPolicy88902FDF)
CdkS3LambdaStack | 8 | 21:07:45 | DELETE_IN_PROGRESS | AWS::IAM::Role | Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C/ServiceRole (CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756CServiceRole89A01265)
CdkS3LambdaStack | 9 | 21:07:45 | DELETE_COMPLETE | AWS::Lambda::Function | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
CdkS3LambdaStack | 9 | 21:07:46 | DELETE_IN_PROGRESS | AWS::IAM::Role | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092)
CdkS3LambdaStack | 9 | 21:07:46 | DELETE_IN_PROGRESS | AWS::S3::Bucket | MyBucket (MyBucketF68F3FF0)
CdkS3LambdaStack | 10 | 21:07:47 | DELETE_COMPLETE | AWS::S3::Bucket | MyBucket (MyBucketF68F3FF0)
✅ CdkS3LambdaStack: destroyed
感想
TypeScriptから勉強しないとよくわからないな、、、( ̄д ̄|||;