Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(experimental-ec2-pattern): Create Policy first #2464

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/popular-laws-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@guardian/cdk": patch
---

fix(experimental-ec2-pattern): Create Policy first

When deploying Prism with the `GuEc2AppExperimental` for the first time, the deployment failed with the cloud-init-output logs stating:

```log
An error occurred (AccessDenied) when calling the DescribeTargetHealth operation: User: arn:aws:sts::000000000000:assumed-role/prism-CODE-InstanceRolePrism/i-0cee86d64de253ca4 is not authorized to perform: elasticloadbalancing:DescribeTargetHealth because no identity-based policy allows the elasticloadbalancing:DescribeTargetHealth action
```

This suggests the instance update was started before the policy was created.

Make the ASG depend on the policy that grants these permissions to resolve, as CloudFormation creates dependencies first.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ exports[`The GuEc2AppExperimental pattern matches the snapshot 1`] = `
"Timeout": "PT3M",
},
},
"DependsOn": [
"AsgRollingUpdatePolicy2A1DDC6F",
],
"Properties": {
"DesiredCapacity": "1",
"HealthCheckGracePeriod": 120,
Expand Down
9 changes: 8 additions & 1 deletion src/experimental/patterns/ec2-app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IAspect } from "aws-cdk-lib";
import { Aspects, CfnParameter, Duration } from "aws-cdk-lib";
import { CfnAutoScalingGroup, CfnScalingPolicy, ScalingProcess, UpdatePolicy } from "aws-cdk-lib/aws-autoscaling";
import type { CfnPolicy } from "aws-cdk-lib/aws-iam";
import { Effect, Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
import type { IConstruct } from "constructs";
import { GuAutoScalingGroup } from "../../constructs/autoscaling";
Expand Down Expand Up @@ -308,7 +309,13 @@ export class GuEc2AppExperimental extends GuEc2App {
},
};

AsgRollingUpdatePolicy.getInstance(scope).attachToRole(role);
const policy = AsgRollingUpdatePolicy.getInstance(scope);
policy.attachToRole(role);

// Create the Policy with necessary permissions first.
// Then create the ASG that requires the permissions.
const cfnPolicy = policy.node.defaultChild as CfnPolicy;
cfnAutoScalingGroup.addDependency(cfnPolicy);

/*
`aws` is available via AMIgo baked AMIs.
Expand Down