AWS CDKでVPCエンドポイントをデプロイする方法
| 項目 | バージョン |
|---|---|
| CDK | 2.27.0 |
AWS CDKでVPCエンドポイントをデプロイする方法です。
VPCエンドポイント作成
VPCエンドポイントで以下5つ作成します。
- com.amazonaws.ap-northeast-1.ssm
- com.amazonaws.ap-northeast-1.ssmmessages
- com.amazonaws.ap-northeast-1.ec2messages
- com.amazonaws.ap-northeast-1.s3 (ゲートウェイ)
- com.amazonaws.ap-northeast-1.secretsmanager
tsファイルで以下のように3つ作成します。
import { Stack, StackProps, aws_secretsmanager } from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
export class Sample001Stack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'vpc-sample-cdk', {
cidr: '10.0.0.0/16',
natGateways: 0,
maxAzs: 2,
subnetConfiguration: [
{
name: 'public-subnet',
subnetType: ec2.SubnetType.PUBLIC,
cidrMask: 24,
},
{
name: 'isolated-subnet',
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
cidrMask: 24,
}
]
});
// VPC Endpoint
const endpoints : Array<[ec2.InterfaceVpcEndpointAwsService, string]> =
[[ec2.InterfaceVpcEndpointAwsService.SSM, 'ssm'],
[ec2.InterfaceVpcEndpointAwsService.EC2_MESSAGES, 'ec2messages'],
[ec2.InterfaceVpcEndpointAwsService.SSM_MESSAGES, 'ssmmessages']]
for(const data of endpoints) {
new ec2.InterfaceVpcEndpoint(this, data[1], {
vpc,
service: data[0],
subnets: {
subnetType: ec2.SubnetType.PUBLIC // 明示的に指定
},
privateDnsEnabled: true // プライベートDNS有効化
});
}
const subnetSelection: ec2.SubnetSelection = {
subnetType: ec2.SubnetType.PRIVATE_ISOLATED, onePerAz: true
};
new ec2.GatewayVpcEndpoint(this, 's3gateway', {
vpc,
service: ec2.GatewayVpcEndpointAwsService.S3,
subnets: [subnetSelection]
});
new ec2.InterfaceVpcEndpoint(this, 'secretmanager', {
vpc: vpc,
service: ec2.InterfaceVpcEndpointAwsService.SECRETS_MANAGER,
subnets: {
subnetType: ec2.SubnetType.PRIVATE_ISOLATED
}
});
}
}
これでデプロイします。
cdk deploy
デプロイに255.83sかかりました、VPCエンドポイントが作成されます。
インタフェース型のエンドポイントの各セキュリティグループのインバウンドは443が解放されています。
参考サイト

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^



コメント