Skip to content

Commit

Permalink
Add initial app infrastructure configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sikleber committed May 5, 2024
1 parent 87fcb87 commit cea4e3b
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ jobs:
run: npm run build

- name: CDK Deploy
run: npm run deploy -- --require-approval never
run: npm run deploy -- --require-approval never --context config=dev
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ jobs:
run: npm run build

- name: CDK Synth
run: npm run synth
run: npm run synth -- --context config=dev
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ See the [infrastructure package.json](infrastructure/package.json) for relevant

Deploy the infrastructure using the following commands:
```bash
npm run deploy -- --profile <profile> --region <region>
npm run deploy -- --profile <profile> --region <region> --context config=dev
```

The [cdk.json](infrastructure/cdk.json) file tells the CDK Toolkit how to execute your app.

### Infrastructure Overview
<img alt="Infrastructure" src="docs/infrastructure.drawio.png" style="width: 50%;">

### Custom Configuration Settings
The [infrastructure/config.ts](infrastructure/src/config.ts) reads the configuration specified by the `--context config=<CONFIG_NAME` parameter when deploying the CDK stack.
Defined settings can be passed to underlying constructs.


## GitHub Actions CI/CD
This project uses GitHub Actions for CI/CD. Two workflows are defined in the [.github/workflows](.github/workflows) directory:
- [ci.yml](.github/workflows/ci.yml) - runs on every push to any branch except `main`
Expand Down
3 changes: 3 additions & 0 deletions config/dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"removalPolicy": "destroy"
}
3 changes: 3 additions & 0 deletions config/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"removalPolicy": "destroy"
}
7 changes: 5 additions & 2 deletions infrastructure/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/src/env node
import 'source-map-support/register'
import * as cdk from 'aws-cdk-lib'
import { ReactAppStack } from './reactAppStack'
import { ReactAppStack } from './stacks/reactAppStack'
import { getAppConfig } from './config'

const app = new cdk.App()

new ReactAppStack(app, 'ReactAppStack', {})
const config = getAppConfig(app.node.getContext('config') as string)

new ReactAppStack(app, 'ReactAppStack', config)

app.synth()
21 changes: 21 additions & 0 deletions infrastructure/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as cdk from 'aws-cdk-lib'

export interface AppConfig {
readonly removalPolicy: cdk.RemovalPolicy
}

const defaultAppConfig: AppConfig = {
removalPolicy: cdk.RemovalPolicy.RETAIN
}

/**
* Reads the json configuration file for the given name and overrides the default configuration.
*
* @param name The name of the configuration file to read.
*/
export function getAppConfig(name?: string): AppConfig {
return {
...defaultAppConfig,
...require(`../../config/${name}.json`)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import * as s3 from 'aws-cdk-lib/aws-s3'
import * as s3_deploy from 'aws-cdk-lib/aws-s3-deployment'
import * as cf from 'aws-cdk-lib/aws-cloudfront'
import * as cf_origin from 'aws-cdk-lib/aws-cloudfront-origins'
import { AppConfig } from '../config'

export class ReactAppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
constructor(
scope: Construct,
id: string,
config: AppConfig,
props?: cdk.StackProps
) {
super(scope, id, props)

const deploymentBucket = new s3.Bucket(this, 'ReactAppDeploymentBucket')
const deploymentBucket = new s3.Bucket(this, 'ReactAppDeploymentBucket', {
removalPolicy: config.removalPolicy
})
new s3_deploy.BucketDeployment(this, 'ReactAppDeployment', {
sources: [s3_deploy.Source.asset('../frontend/dist')],
destinationBucket: deploymentBucket
Expand Down
16 changes: 16 additions & 0 deletions infrastructure/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AppConfig, getAppConfig } from '../src/config'
import * as cdk from 'aws-cdk-lib'

let config: AppConfig

beforeAll(() => {
config = getAppConfig('test')
})

describe('Config', () => {
it('should equal test config', () => {
expect(config).toEqual({
removalPolicy: cdk.RemovalPolicy.DESTROY
})
})
})
6 changes: 6 additions & 0 deletions infrastructure/test/globalFixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AppConfig } from '../src/config'
import * as cdk from 'aws-cdk-lib'

export const testConfig: AppConfig = {
removalPolicy: cdk.RemovalPolicy.DESTROY
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Match, Template } from 'aws-cdk-lib/assertions'
import * as cdk from 'aws-cdk-lib';
import { ReactAppStack } from '../src/reactAppStack'
import { ReactAppStack } from '../../src/stacks/reactAppStack'
import { testConfig } from '../globalFixtures'

let template: Template;

beforeAll(() => {
const app = new cdk.App();
const stack = new ReactAppStack(app, 'TestReactAppStack');
const stack = new ReactAppStack(app, 'TestReactAppStack', testConfig);
template = Template.fromStack(stack);
})

describe('Deployment Bucket', () => {
it('should create a S3 bucket', () => {
template.hasResource('AWS::S3::Bucket', {
Type: 'AWS::S3::Bucket',
DeletionPolicy: 'Delete'
})
})

Expand Down

0 comments on commit cea4e3b

Please sign in to comment.