# Suno CDK
## Setup


> [!IMPORTANT]  
> The CDK is auto deployed through to prod after merging with the [suno-cdk-pipeline in suno-dev](https://us-east-2.console.aws.amazon.com/codesuite/codepipeline/pipelines/suno-cdk-pipeline/view?region=us-east-2), so this setup and deploy info is mostly obsolete. It's just here now in case you ever end up needing to do something manually.


- Get access to PowerUser role access from Rider / AWS Admin
- Log into your staging / prod AWS account with the new PowerUser role
- Create an admin user in IAM (copy permissions over from existing admin user)
- Create an access key and run the following to configure your local setup:
  - for staging token: `aws configure --profile staging`
  - for prod token: `aws configure --profile prod`
  - Note: you may need to [install the aws cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) to do this
  - You'll want to end up with entries like in `~/.aws/credentials`:
```
[staging]
aws_access_key_id = ...
aws_secret_access_key = ...

[prod]
aws_access_key_id = ...
aws_secret_access_key = ...
```
- Also configure the default region in `~/.aws/config` as listed below to avoid issues:

```
[default]
region = us-east-2
```

## How to make changes
- make code changes to set up the resources / infra you want
- change the `profile` value in `suno-cdk.ts` to `staging`
- cd into the `suno-cdk` folder and run `npm install` to install the packages in package-lock.json if you haven't already
- ensure Docker is running
- run `cdk synth --profile staging STACK_NAME` to synthesize the changes against staging
- run `cdk diff --profile staging STACK_NAME` to get a diff with what is currently deployed
  - sanity check to make sure all the changes are what you expect
- open a PR and get it approved
  - bonus: add the out diff to the PR description
- once approved: run `cdk deploy --profile staging STACK_NAME` to deploy the changes to staging

### Deploying to prod
- change the `profile` value in `suno-cdk.ts` to `prod`
- run `cdk deploy --profile prod STACK_NAME` to deploy the changes to prod
- make sure to deploy to all the regions: 
  - change the `REGION` value in `suno-cdk.ts` to `us-east-2` / `ap-southeast-1` / `eu-central-1`, depending on what is remaining
  - run `cdk deploy --profile prod STACK_NAME` again to deploy the changes to prod

## FAQ
- If you run into `Error [ConfigError]: Missing region in config` when running the cdk commands, add a `[default]` profile to your `~/.aws/config` file:

```
[default]
region = us-east-2
output = json
```

- If you run into an error complaining your account doesn't have permissions to assume a role, you likely are trying to deploy `prod` with `staging` set in `suno-cdk.ts` or vice-versa. You can check the AWS account IDs in the error message ARNs and see that they are mismatched to confirm.

## Useful commands

* `npm run build`   compile typescript to js
* `npm run watch`   watch for changes and compile
* `npm run test`    perform the jest unit tests
* `npx cdk synth`   emits the synthesized CloudFormation template
* `npx cdk diff`    compare deployed stack with current state
* `npx cdk deploy`  deploy this stack to your default AWS account/region
