Skip to content

Conversation

@Tietew
Copy link
Contributor

@Tietew Tietew commented Feb 27, 2025

Issue # (if applicable)

Closes #33584.

Reason for this change

AWS CodePipeline introduces a new action to deploy to Amazon Elastic Compute Cloud (EC2).
https://aws.amazon.com/about-aws/whats-new/2025/02/aws-codepipeline-native-ec2-deployment-support/

Description of changes

Added the Ec2DeployAction action class and corresponding helpers.

  • Ec2InstanceType - specify instance type: EC2 or SSM_MANAGED_NODE
  • Ec2DeploySpecification - choose deploy specification: inline or DeploySpec (not yet included)
  • Ec2MaxInstances - specify maxBatch and maxError configuration

Usage

new cpactions.Ec2DeployAction({
  actionName: 'EC2',
  input: buildOutput,
  // specify instance type
  instanceType: cpactions.Ec2InstanceType.EC2,  // REQUIRED
  // specify tag key and value, not ec2.IInstance
  instanceTagKey: 'Target',                     // REQUIRED
  instanceTagValue: 'DeployTarget',
  // deploy specifications
  deploySpecifications: cpactions.Ec2DeploySpecifications.inline({
    targetDirectory: '/home/ec2-user/deploy',   // REQUIRED
    preScript: 'hooks/pre-script',
    postScript: 'hooks/post-script',            // REQUIRED
  }),
  // the action will detach and attach instances from/to target groups
  targetGroups: [myTargetGroup],
  // the number or percentage of instances that can deploy in parallel
  maxBatch: cpactions.Ec2MaxInstances.target(2),
  maxError: cpactions.Ec2MaxInstances.percent(50),
});

Describe any new or updated permissions being added

Ec2DeployAction adds permissions based on CodePipeline documentation:
https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-EC2Deploy.html#action-reference-EC2Deploy-permissions-action

For details of actions, resource, and condition keys, see the Service Authorization Reference: EC2, ELBv2, SSM

Description of how you validated changes

Unit tests and an integ test.
The integ test also asserts pipeline execution.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Feb 27, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team February 27, 2025 08:58
@github-actions github-actions bot added the star-contributor [Pilot] contributed between 25-49 PRs to the CDK label Feb 27, 2025
@codecov
Copy link

codecov bot commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.98%. Comparing base (994e952) to head (ea61e43).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #33604   +/-   ##
=======================================
  Coverage   83.98%   83.98%           
=======================================
  Files         120      120           
  Lines        6976     6976           
  Branches     1178     1178           
=======================================
  Hits         5859     5859           
  Misses       1005     1005           
  Partials      112      112           
Flag Coverage Δ
suite.unit 83.98% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk ∅ <ø> (∅)
packages/aws-cdk-lib/core 83.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

/**
* Max number of instances.
*
* Valid range: from 1 to number of your instances
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation based on AWS console:
image

/**
* Max percentage of instances.
*
* Valid range: from 1 to 99
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation based on AWS console:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've succeeded to deploy with maxBatch: '100%'. The note of the AWS console seems inaccurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated max allowed percentage to 100%.

TargetDirectory: this.props.targetDirectory,
MaxBatch: this.props.maxBatch?.value,
MaxError: this.props.maxError?.value,
TargetGroupNameList: this.props.targetGroups?.length ? this.props.targetGroups.map((tg) => tg.targetGroupName).join(',') : undefined,
Copy link
Contributor Author

@Tietew Tietew Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From AWS console, TargetGroupListName is a comma-separated string instead of a JSON array.

Result of GetPipeline: (pipeline created by console)
image

if (!Token.isUnresolved(percentage) && !(percentage >= 1 && percentage <= 99 && Number.isInteger(percentage))) {
throw new UnscopedValidationError(`percentage must be a positive integer between 1 and 99. got ${percentage}`);
}
return { value: `${Tokenization.stringifyNumber(percentage)}%` };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From AWS console, percentage is represented as "NN%"
image

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Feb 27, 2025
@Tietew
Copy link
Contributor Author

Tietew commented Mar 17, 2025

snapshot change is caused by #33742

@Tietew
Copy link
Contributor Author

Tietew commented May 21, 2025

@Tietew Tietew marked this pull request as draft May 21, 2025 07:34
Copy link
Contributor

@kumvprat kumvprat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments on the PR

'ec2:DescribeInstances',
'elasticloadbalancing:DescribeTargetGroupAttributes',
'elasticloadbalancing:DescribeTargetGroups',
'elasticloadbalancing:DescribeTargetHealth',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ssm:CancelCommand may need to be scoped down, similar to ssm:SendCommand, assuming it can cancel the post/pre-scripts running on the specific instance.

Copy link
Contributor Author

@Tietew Tietew May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, looks like ssm:CancelCommand has been added as part of the deploy spec support...
I'll reflect it anyway.

Copy link
Contributor Author

@Tietew Tietew May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misunderstood your comment.

These policy statements are described in Amazon EC2 action reference.

The ssm:CancelCommand has no related resource types and resources field must be [*].
See https://docs.aws.amazon.com/service-authorization/latest/reference/list_awssystemsmanager.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating this
We could add this link : https://docs.aws.amazon.com/service-authorization/latest/reference/list_awssystemsmanager.html and for the related elasticloadbalancing to the PR description/README

Would be nice for users to know about IAM actions

@aws aws deleted a comment from kumvprat May 21, 2025
Copy link
Contributor

@kumvprat kumvprat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the PR looks good, except for a few remaining changes.

Is there a specific reason for clubbing the deploy spec support in this PR ?
Releasing it as a follow-up PR would also work

integ.assertions
.awsApiCall('codepipeline', 'GetPipelineExecution', { pipelineName: ssmPipeline.pipelineName, pipelineExecutionId: ssmPipelineExecutionId })
.waitForAssertions({ interval: cdk.Duration.seconds(30) })
.expect(ExpectedResult.objectLike({ pipelineExecution: { status: 'Succeeded' } }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add the same checks for SSM_MANAGED_NODES and EC2 one ?
Is there some difference between the usages of the two instance types ? If so maybe we need to update the README example to reflect this

These checks seem to be missing from SSM_MANAGED_NODE types :

waitPipelieneSuccess.next(
integ.assertions
.httpApiCall(http://${alb.loadBalancerDnsName}/LB/index.html)
.expect(ExpectedResult.objectLike({ status: 200 })),
);
instances.NoLB.forEach((instance) => waitPipelieneSuccess.next(
integ.assertions
.httpApiCall(http://${instance.instancePublicDnsName}/NoLB/index.html)
.expect(ExpectedResult.objectLike({ status: 200 })),
));

Copy link
Contributor Author

@Tietew Tietew May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, SSM_MANAGED_NODES are on-premise instances outside of AWS.
These instances cannot be created in integ test.
(please correct me if wrong.)

The EC2 deploy action will succeed if no instances match.
So I only verify whether the pipeline succeeds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that SSM_MANAGED_NODE matches EC2 instances.
I've made separate 2 integ tests with EC2 and SSM_MANAGED_NODE.

'ec2:DescribeInstances',
'elasticloadbalancing:DescribeTargetGroupAttributes',
'elasticloadbalancing:DescribeTargetGroups',
'elasticloadbalancing:DescribeTargetHealth',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating this
We could add this link : https://docs.aws.amazon.com/service-authorization/latest/reference/list_awssystemsmanager.html and for the related elasticloadbalancing to the PR description/README

Would be nice for users to know about IAM actions

@Tietew
Copy link
Contributor Author

Tietew commented May 27, 2025

Is there a specific reason for clubbing the deploy spec support in this PR ?

DeploySpec support will introduce the "choice" which deployment style are used.
At least I want to supply bind()-style integration class.
https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md#unions

I'll drop deploySpec support in this PR if needed.

@Tietew
Copy link
Contributor Author

Tietew commented May 29, 2025

I could not succeed to deploy with deployspec.yml currently.
Therefore, I'll drop DeploySpec support in this PR...

@Tietew Tietew marked this pull request as ready for review May 29, 2025 07:08
@Tietew
Copy link
Contributor Author

Tietew commented May 29, 2025

@kumvprat
Thanks for review!

I've pushed updates and marked as ready.
The DeploySpec support will be a separate PR.

We could add this link : https://docs.aws.amazon.com/service-authorization/latest/reference/list_awssystemsmanager.html and for the related elasticloadbalancing to the PR description/README

I've added refs in PR description and code comments.
But I didn't add to README because it doesn't explain any IAM permissions CDK will grant.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label May 29, 2025
@kumvprat kumvprat self-assigned this Jun 4, 2025
Copy link
Contributor

@kumvprat kumvprat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mergify
Copy link
Contributor

mergify bot commented Jun 5, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jun 5, 2025
@mergify
Copy link
Contributor

mergify bot commented Jun 5, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 558af95
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Jun 5, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 9d39db7 into aws:main Jun 5, 2025
16 checks passed
@github-actions
Copy link
Contributor

github-actions bot commented Jun 5, 2025

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2025
@Tietew Tietew deleted the codepipeline-ec2deploy branch June 5, 2025 22:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 star-contributor [Pilot] contributed between 25-49 PRs to the CDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(codepipeline): native Amazon EC2 deployment support

5 participants