1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CDKでCodeDeployによるECSデプロイ構築できるようになったみたいなので試す

Last updated at Posted at 2022-11-13

背景

ここ最近までCDKでCodeDeployをL2で作成できるのはEC2とLambdaのみであったが、ECSもデプロイできるようになったため早速試した。

(Pythonで書いてます)。

つくったコード

CodeDeploy部分のコード

いままで手動で作成していたアプリケーション、デプロイメントグループを定義できる。
ECSサービスとメインリスナー、テストリスナーを指定する。

# アプリケーション
ecs_application = codedeploy.EcsApplication(self, "CodeDeployApplication",
                                            application_name="MyApplication"
                                            )
# デプロイメントグループ
ecs_deployment_group = codedeploy.EcsDeploymentGroup(
    self, "BlueGreenDG",
    application=ecs_application,
    service=service,
    blue_green_deployment_config=codedeploy.EcsBlueGreenDeploymentConfig(
        blue_target_group=blue_target_group,
        green_target_group=green_target_group,
        listener=main_listener,
        test_listener=test_listener,
        deployment_approval_wait_time=Duration.minutes(30),
        termination_wait_time=Duration.minutes(30),
    ),
    deployment_config=codedeploy.EcsDeploymentConfig.ALL_AT_ONCE,
)

CodePipeline部分。パイプラインの構成にもよるが今回はECRとS3をSourceとして、S3にappspec.ymlとtaskdef.jsonを格納する方式にしたため、それぞれのファイル場所を指定した。imageDetail.jsonはECRが自動生成してくれるアーティストを指定した。


# deploy
code_deploy_ecs_deploy_action = codepipeline_actions.CodeDeployEcsDeployAction(
    action_name="actionName",
    deployment_group=ecs_deployment_group,
    app_spec_template_file=s3_output.at_path('appspec.yml'),
    task_definition_template_file=s3_output.at_path('taskdef.json'),
    container_image_inputs=[codepipeline_actions.CodeDeployEcsContainerImageInput(
        input=ecr_output,
        task_definition_placeholder="IMAGE1_NAME"
    )],
)
pipeline.add_stage(
    stage_name="Deploy",
    actions=[code_deploy_ecs_deploy_action]
)

感想

いままで手動でつくらなければならなかったので、これからは手軽に作ったり消したりできて嬉しい。

リンク

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?