aws / aws/aws-cdk

(aws-codestarnotifications): Creating SNS Notification rule fails when service account does not exist in the account

Open
#34,406 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-codestarnotifications bug p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

When setting up notifications for Codepipeline rule on a Codepipeline where SNS is used as a target,
a [codestar notifications service linked role](https://docs.aws.amazon.com/dtconsole/latest/userguide/using-service-linked-roles.html#:~:text=A%20service%2Dlinked%20role%20is,AWS%20services%20on%20your%20behalf.) needs to be available in the account otherwise the notification rule will fail to be created.

If the service linked role is not present (usually the case if notifications on a pipeline is being set up for the first time on an account) the service linked role is created automatically by Cloudformation which usually takes a bit of time. However before the service linked role has been created, Cloudformation goes off and attempts to create the Notification rule which fails as the service linked role is not yet present.

When I check the `CreateNotificationRule` event in Cloudtrail, I see the following error message in the `responseElement`

```
"AWS CodeStar Notifications could not create the AWS CloudWatch Events managed rule in your AWS account. If this is your first time creating a notification rule, the service-linked role for AWS CodeStar Notifications might not yet exist. Creation of this role might take up to 15 minutes. Until it exists, notification rule creation will fail. Wait 15 minutes, and then try again.
If this is is not the first time you are creating a notification rule, there might be a problem with a network connection, or one or more AWS services might be experiencing issues.
Verify your network connection and check to see if there are any issues with AWS services in your AWS Region before trying again."
```

There has been previous reports of this issue where it was assumed that adding a dependency on the topic role
would fix the issue but the actual root of the problem is that the service linked role needs to be created first. (https://github.com/aws/aws-cdk/issues/29484)

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Library Version

_No response_

### Expected Behavior

Deployment is successful

### Current Behavior

Deployment fails and get the following error in Cloudformation:
`Invalid request provided: AWS::CodeStarNotifications::NotificationRule`

### Reproduction Steps

1. Ensure there is no codestar notifications service linked role in the aws account (if there is one delete it)
2. Attempt to deploy a pipeline with a notification rule where an SNS topic is set as the target e.g
```
const pipeline = new codepipeline.Pipeline(this, 'Pipeline', {
pipelineName: 'MyPipeline',
...
});
const topic = new sns.Topic(this, 'Topic');
const rule = new NotificationRule(this, 'NotificationRule', {
...,
targets: [topic]
});
```
3. Deployment should fail and when you check the CreateNotificationRule event in Cloudtrail you should see the error message in the responseElement that it failed to create the CloudWatch Events managed rule

### Possible Solution

A possible way round this is to have a custom resource that goes off and creates the service linked role and
add a dependency on the notification rule so that the service linked role is created before creating the notification rule.

However, it would be nice if there is a much simpler solution to this issue and open to suggestions. (is it possible to add a dependency on a service link role without using a custom resource?)

(I'm also happy to create attempt to PR to fix this issue once a solution is agreed upon)

### Additional Information/Context

_No response_

### AWS CDK Library version (aws-cdk-lib)

2.189.0

### AWS CDK CLI version

2.1007.0

### Node.js Version

20

### OS

MacOS

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the aws-codestar-notifications NotificationRule construct and its CloudFormation resource dependencies; reproduce the deployment with the service-linked role absent. Done means the role is available before the notification rule is created and the first deployment with an SNS target succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.