Skip to content

Commit

Permalink
Merge pull request #268 from aws-samples/feature/minor_code_update
Browse files Browse the repository at this point in the history
Feature/minor code update
  • Loading branch information
kzarms committed Jan 11, 2024
2 parents f52fa7c + a828626 commit 509a6af
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ In the Dev stage there are 3 steps `Linting`, `Security` and `Unit Tests`. These
## Prerequsites & Limitations

This project use AWS CDK v2 based on typescript. The developer laptop/computer should have following software.
* [AWS CDK v2](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) v2.61.0
* [AWS CDK v2](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) v2.118.0
* [cfn_nag](https://github.com/stelligent/cfn_nag) v0.8.10
* [git-remote-codecommit](https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-git-remote-codecommit.html) v1.16
* [node](https://nodejs.org/en/download/) v19
* [typescript](https://www.typescriptlang.org/) v4.9.4
* [node](https://nodejs.org/en/download/) v20
* [typescript](https://www.typescriptlang.org/) v5.3.3

Limitation

Expand Down
8 changes: 8 additions & 0 deletions lib/main-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export class MainStack extends Stack {
// Create a sample event bus
const eventBus = new EventBus(this, 'EventBus')

/**
* Example of different behavior based on the stack name.
* Create the second event bus if it is a 'dev' deployment.
*/
if (this.stackName.toLowerCase().includes('dev')) {
new EventBus(this, 'EventBus2')
}

// Output
new CfnOutput(this, 'EventBusName', {
value: eventBus.eventBusName
Expand Down
2 changes: 2 additions & 0 deletions lib/pipeline-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class CodePipelineStack extends Stack {
})

const pipeline = new CodePipeline(this, 'Pipeline', {
crossAccountKeys: true,
enableKeyRotation: true,
synth: new ShellStep('Synth', {
input: CodePipelineSource.codeCommit(repo, 'main'),
installCommands: [
Expand Down
48 changes: 39 additions & 9 deletions test/main-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,46 @@ import * as cdk from 'aws-cdk-lib'
import { Template } from 'aws-cdk-lib/assertions'
import { MainStack } from '../lib/main-stack'

const app = new cdk.App()
const stack = new MainStack(app, 'Dev-MainStack')
const template = Template.fromStack(stack)
describe('Unit tests for the main stack in DEV', () => {
const app = new cdk.App()
const stack = new MainStack(app, 'Dev-MainStack')
const template = Template.fromStack(stack)

test('Event Bus has been created', () => {
// Assessment
template.hasResource('AWS::Events::EventBus', '')
test('Event Bus has been created', () => {
// Assessment
template.hasResource('AWS::Events::EventBus', '')
})

test('MainStack has output', () => {
// Assessment
template.hasOutput('EventBusName', '')
})

// Summary checks
test('Expected number of the event buses', () => {
const expectedValue = 2 // Two event busses are expected
template.resourceCountIs('AWS::Events::EventBus', expectedValue)
})
})

test('MainStack has output', () => {
// Assessment
template.hasOutput('EventBusName', '')
describe('Unit tests for the main stack in PROD', () => {
const app = new cdk.App()
const stack = new MainStack(app, 'Prod-MainStack')
const template = Template.fromStack(stack)

test('Event Bus has been created', () => {
// Assessment
template.hasResource('AWS::Events::EventBus', '')
})

test('MainStack has output', () => {
// Assessment
template.hasOutput('EventBusName', '')
})

// Summary checks
test('Expected number of the event buses', () => {
const expectedValue = 1 // Only one event bus is expected
template.resourceCountIs('AWS::Events::EventBus', expectedValue)
})
})
98 changes: 81 additions & 17 deletions test/pipeline-stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,89 @@
import * as cdk from 'aws-cdk-lib'
import { Template } from 'aws-cdk-lib/assertions'
import { Match, Template } from 'aws-cdk-lib/assertions'
import { CodePipelineStack } from '../lib/pipeline-stack'

const app = new cdk.App()
const stack = new CodePipelineStack(app, 'CodePipeline')
const template = Template.fromStack(stack)
describe('Unit tests for the pipeline stack', () => {
const app = new cdk.App()
const stack = new CodePipelineStack(app, 'CodePipeline')
const template = Template.fromStack(stack)

// Execute tests for CodePipeline template
test('Pipeline restarts on update', () => {
// Assessment
template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
RestartExecutionOnUpdate: true
// Execute tests for CodePipeline template
test('Pipeline restarts on update', () => {
template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
RestartExecutionOnUpdate: true
})
})
})

test('There are 8 CodeBuild objects in use', () => {
// Assessment
template.resourceCountIs('AWS::CodeBuild::Project', 8)
})
test('Key rotation is enabled', () => {
template.hasResourceProperties('AWS::KMS::Key', {
EnableKeyRotation: true
})
})

test('Pipeline source settings', () => {
template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([
{
Actions: [{
ActionTypeId: {
Category: 'Source',
Owner: 'AWS',
Provider: 'CodeCommit',
Version: '1'
},
Configuration: {
BranchName: 'main',
PollForSourceChanges: false,
RepositoryName: Match.anyValue()
},
Name: Match.anyValue(),
OutputArtifacts: Match.anyValue(),
RoleArn: Match.anyValue(),
RunOrder: 1
}],
Name: 'Source'
}]
)
})
})

test('Pipeline build settings', () => {
template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([
{
Actions: [{
ActionTypeId: {
Category: 'Build',
Owner: 'AWS',
Provider: 'CodeBuild',
Version: '1'
},
Configuration: Match.anyValue(),
InputArtifacts: Match.anyValue(),
Name: 'Synth',
OutputArtifacts: [{ Name: 'Synth_Output' }],
RoleArn: Match.anyValue(),
RunOrder: 1
}],
Name: 'Build'
}]
)
})
})

// Summary checks
test('Expected number of the CodeBuild objects', () => {
const expectedValue = 8
template.resourceCountIs('AWS::CodeBuild::Project', expectedValue)
})

test('CodePipeline has repository name in output', () => {
// Assessment
template.hasOutput('RepositoryName', '')
test('Expected number of the S3 buckets', () => {
const expectedValue = 1
template.resourceCountIs('AWS::S3::Bucket', expectedValue)
})

test('Expected number of IAM roles', () => {
const expectedValue = 12
template.resourceCountIs('AWS::IAM::Role', expectedValue)
})
})

0 comments on commit 509a6af

Please sign in to comment.