概要
AWS CDK v2.186.0では、Cognito Identity PoolとSchedulerモジュールの安定版への昇格が行われました。また、CodePipelineでステージレベルの条件実行機能やGitプッシュフィルターの拡張、Lambda Ruby 3.4のサポート、EC2でのPrefix List Lookup機能が追加されています。
新機能
Cognito Identity Poolの安定版への昇格 (PR #33905)
@aws-cdk/aws-cognito-identitypool-alphaモジュールが安定版に昇格し、aws-cdk-lib/aws-cognito-identitypoolとして利用可能になりました。これにより、Cognito Identity Poolを本番環境で安心して使用できるようになります。
import * as cognito from 'aws-cdk-lib/aws-cognito';
import * as cognitoidentitypool from 'aws-cdk-lib/aws-cognito-identitypool';
const userPool = new cognito.UserPool(this, 'UserPool');
const userPoolClient = userPool.addClient('Client');
// Identity Poolの作成
const identityPool = new cognitoidentitypool.IdentityPool(this, 'IdentityPool', {
// 認証されたユーザーのロールを自動作成
allowUnauthenticatedIdentities: false,
authenticationProviders: {
userPools: [
new cognitoidentitypool.UserPoolAuthenticationProvider({
userPool: userPool,
userPoolClient: userPoolClient,
}),
],
},
});
// 認証されたユーザーのロールを取得
const authenticatedRole = identityPool.authenticatedRole;
Scheduler/Scheduler Targetsの安定版への昇格 (PR #33903)
@aws-cdk/aws-scheduler-alphaと@aws-cdk/aws-scheduler-targets-alphaモジュールが安定版に昇格し、それぞれaws-cdk-lib/aws-schedulerとaws-cdk-lib/aws-scheduler-targetsとして利用可能になりました。非推奨だったGroupコンストラクトは削除され、代わりにScheduleGroupを使用する必要があります。
import * as scheduler from 'aws-cdk-lib/aws-scheduler';
import * as targets from 'aws-cdk-lib/aws-scheduler-targets';
import * as lambda from 'aws-cdk-lib/aws-lambda';
// スケジュールグループの作成
const scheduleGroup = new scheduler.ScheduleGroup(this, 'ScheduleGroup', {
scheduleGroupName: 'MyScheduleGroup',
});
const myFunction = lambda.Function.fromFunctionArn(
this,
'Function',
'arn:aws:lambda:us-east-1:123456789012:function:MyFunction'
);
// Lambdaを定期実行するスケジュール
new scheduler.Schedule(this, 'Schedule', {
schedule: scheduler.ScheduleExpression.rate(Duration.minutes(5)),
target: new targets.LambdaInvoke(myFunction, {
input: scheduler.ScheduleTargetInput.fromObject({
key: 'value',
}),
}),
group: scheduleGroup,
});
Scheduler TargetsでECSタスクの実行をサポート (PR #33697)
EventBridge SchedulerのターゲットとしてECSタスクの実行がサポートされました。FargateとEC2起動タイプの両方に対応しています。
import * as scheduler from 'aws-cdk-lib/aws-scheduler';
import * as targets from 'aws-cdk-lib/aws-scheduler-targets';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
const vpc = new ec2.Vpc(this, 'Vpc');
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
memoryLimitMiB: 512,
cpu: 256,
});
taskDefinition.addContainer('AppContainer', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});
// Fargateタスクのスケジュール実行
new scheduler.Schedule(this, 'Schedule', {
schedule: scheduler.ScheduleExpression.rate(Duration.hours(1)),
target: new targets.EcsRunFargateTask({
cluster,
taskDefinition,
// サブネットの選択
subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
// プラットフォームバージョンの指定
platformVersion: ecs.FargatePlatformVersion.LATEST,
// タグの伝播を有効化
propagateTags: true,
// カスタムタグの追加
tags: [
{ key: 'Environment', value: 'Production' },
],
}),
});
// EC2タスクのスケジュール実行
const ec2TaskDefinition = new ecs.Ec2TaskDefinition(this, 'Ec2TaskDef');
new scheduler.Schedule(this, 'Ec2Schedule', {
schedule: scheduler.ScheduleExpression.rate(Duration.hours(1)),
target: new targets.EcsRunEc2Task({
cluster,
taskDefinition: ec2TaskDefinition,
// タスク数の指定
taskCount: 2,
// 配置制約の指定
placementConstraints: [
ecs.PlacementConstraint.distinctInstances(),
],
// 配置戦略の指定
placementStrategies: [
ecs.PlacementStrategy.spreadAcross(ecs.BuiltInAttributes.AVAILABILITY_ZONE),
],
}),
});
CodePipeline: ステージレベルの条件実行機能 (PR #33809)
CodePipelineでステージレベルの条件を設定できるようになりました。これにより、特定の条件が満たされた場合にのみステージを実行することができます。
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';
import * as codepipeline_actions from 'aws-cdk-lib/aws-codepipeline-actions';
const pipeline = new codepipeline.Pipeline(this, 'Pipeline', {
pipelineType: codepipeline.PipelineType.V2,
});
const sourceOutput = new codepipeline.Artifact();
const sourceAction = new codepipeline_actions.CodeStarConnectionsSourceAction({
actionName: 'Source',
owner: 'myorg',
repo: 'myrepo',
output: sourceOutput,
connectionArn: 'arn:aws:codestar-connections:us-east-1:123456789012:connection/xxx',
});
pipeline.addStage({
stageName: 'Source',
actions: [sourceAction],
});
// 条件付きステージの追加
const deployStage = pipeline.addStage({
stageName: 'Deploy',
actions: [
new codepipeline_actions.ManualApprovalAction({
actionName: 'Approve',
}),
],
});
// ブランチが'main'の場合のみ実行される条件を設定
deployStage.addCondition(
new codepipeline.Condition({
// 条件名
conditionName: 'MainBranchOnly',
// 成功条件: ソースブランチが'main'の場合
result: codepipeline.ConditionResult.success(
codepipeline.Rule.equal('#{SourceVariables.BranchName}', 'main')
),
})
);
CodePipeline: Gitプッシュフィルターでブランチとファイルのサポート (PR #33872)
CodePipelineのGitプッシュトリガーで、特定のブランチやファイルパスに基づいたフィルタリングが可能になりました。
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';
import * as codepipeline_actions from 'aws-cdk-lib/aws-codepipeline-actions';
const sourceOutput = new codepipeline.Artifact();
const pipeline = new codepipeline.Pipeline(this, 'Pipeline', {
pipelineType: codepipeline.PipelineType.V2,
triggers: [
{
providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION,
gitConfiguration: {
sourceAction: new codepipeline_actions.CodeStarConnectionsSourceAction({
actionName: 'Source',
owner: 'myorg',
repo: 'myrepo',
output: sourceOutput,
connectionArn: 'arn:aws:codestar-connections:us-east-1:123456789012:connection/xxx',
}),
pushFilter: [
{
// 特定のブランチでのみトリガー
branches: {
includes: ['main', 'develop'], // mainまたはdevelopブランチ
excludes: ['feature/*'], // featureブランチは除外
},
// 特定のファイルパスでのみトリガー
filePaths: {
includes: ['src/**', 'lib/**'], // srcまたはlibディレクトリ内の変更
excludes: ['**/*.md'], // Markdownファイルは除外
},
},
],
},
},
],
});
Lambda: Ruby 3.4ランタイムのサポート (PR #33832)
Lambda関数でRuby 3.4ランタイムがサポートされました。
import * as lambda from 'aws-cdk-lib/aws-lambda';
const rubyFunction = new lambda.Function(this, 'RubyFunction', {
// Ruby 3.4ランタイムを指定
runtime: lambda.Runtime.RUBY_3_4,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda'),
});
Alphaモジュールの新機能
EC2: PrefixList.fromLookup()のサポート (PR #33619)
AWS管理のPrefix Listを名前で検索できるPrefixList.fromLookup()メソッドが追加されました。これにより、CloudFrontなどのAWSサービスのIPアドレス範囲を簡単に参照できます。
import * as ec2 from 'aws-cdk-lib/aws-ec2';
// CloudFront Origin-Facing のPrefix Listを名前で検索
const cloudFrontPrefixList = ec2.PrefixList.fromLookup(this, 'CloudFrontPrefixList', {
prefixListName: 'com.amazonaws.global.cloudfront.origin-facing',
});
const vpc = new ec2.Vpc(this, 'Vpc');
// セキュリティグループでPrefix Listを使用
const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
vpc,
});
securityGroup.addIngressRule(
ec2.Peer.prefixList(cloudFrontPrefixList.prefixListId),
ec2.Port.tcp(443),
'Allow HTTPS from CloudFront'
);
EC2: VPCEndpointServiceでサポートされるIPアドレスタイプの指定 (PR #33877)
VPCエンドポイントサービスで、サポートするIPアドレスタイプ(IPv4、IPv6、Dualstack)を指定できるようになりました。
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
const vpc = new ec2.Vpc(this, 'Vpc');
const nlb = new elbv2.NetworkLoadBalancer(this, 'NLB', { vpc });
// VPCエンドポイントサービスの作成
const vpcEndpointService = new ec2.VpcEndpointService(this, 'EndpointService', {
vpcEndpointServiceLoadBalancers: [nlb],
// サポートするIPアドレスタイプを指定
supportedIpAddressTypes: [
ec2.IpAddressType.IPV4, // IPv4をサポート
ec2.IpAddressType.IPV6, // IPv6をサポート
ec2.IpAddressType.DUALSTACK, // DualStackをサポート
],
});
EC2: 不足していた列挙型の追加 (PR #33821)
EC2モジュールで不足していた列挙型が追加されました。
バグ修正
EventBus: grantPutEventsToがサービスプリンシパルを正しく処理 (PR #33729)
EventBus.grantPutEventsToメソッドが、サービスプリンシパルを正しく処理するように修正されました。この修正はフィーチャーフラグの下で有効化されます。
Core: 一部のコンストラクトIDから有効なアーティファクトIDを生成できない問題を修正 (PR #33863)
特定のコンストラクトIDから有効なアーティファクトIDを生成できない問題が修正されました。
コンテキストプロバイダーのignoreErrorOnMissingContextパラメーターの改善 (PR #33875)
コンテキストプロバイダーのignoreErrorOnMissingContextパラメーターの動作が改善されました。
ValidationErrorsによるエラーハンドリングの改善
複数のモジュールで、型付けされていないエラーの代わりにValidationErrorsをスローするように改善されました。これにより、エラーハンドリングがより明確になります。
対象モジュール:
aws-codecommit(PR #33854)aws-codedeploy(PR #33853)aws-codepipeline(PR #33855)aws-config(PR #33869)aws-docdb(PR #33870)aws-dynamodb(PR #33871)aws-ecr-assets(PR #33899)aws-efs(PR #33885)
CloudFormation L1リソースの更新
以下のサービスでL1 CloudFormationリソース定義が更新されました:
破壊的変更(実験的機能)
以下の実験的機能に破壊的変更があります:
- redshiftserverless:
CfnWorkgroup.attrWorkgroupMaxCapacity属性が削除されました - quicksight:
CfnAnalysis.SheetTextBoxProperty.interactions、CfnDashboard.SheetTextBoxProperty.interactions、CfnTemplate.SheetTextBoxProperty.interactionsプロパティが削除されました - imagebuilder:
CfnDistributionConfiguration.DistributionProperty.ssmParameterConfigurationsプロパティが削除されました
まとめ
AWS CDK v2.186.0は、Cognito Identity PoolとSchedulerモジュールの安定版への昇格により、より多くのサービスを本番環境で使用できるようになりました。CodePipelineの新機能により、より柔軟なCI/CDパイプラインの構築が可能になり、Lambda Ruby 3.4のサポートやEC2の新機能により、開発の選択肢が広がっています。