Skip to content

Commit

Permalink
feat: Configure basic Hydro survey stack
Browse files Browse the repository at this point in the history
- Deploy in a separate job for ease of debugging.
- Clarify documentation
  • Loading branch information
l0b0 committed Sep 20, 2023
1 parent 5a5147e commit bf4d82e
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: Build

on:
pull_request:
push:
branches:
- master

jobs:
build:
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)

Code to deploy infrastructure on the "AWS LI Hydro Surveys Prod" account (#434775598764).
Code to deploy AWS LI Hydro surveys infrastructure.

## Use

Authenticate using `aws-azure-login --no-prompt --profile="$AWS_PROFILE"`

- `npm run cdk synth` emits the synthesized CloudFormation template
- `npm run cdk deploy` deploy this stack to your default AWS account/region
- `npm run cdk diff` compare deployed stack with current state
- `npm run cdk synth` emits the synthesized CloudFormation template

## Development

Expand Down
15 changes: 3 additions & 12 deletions bin/hydro-survey.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
#!/usr/bin/env node
import 'source-map-support/register';

import * as cdk from 'aws-cdk-lib';
import { App } from 'aws-cdk-lib';

import { HydroSurveyStack } from '../lib/hydro-survey-stack';

const app = new cdk.App();
const app = new App();
new HydroSurveyStack(app, 'HydroSurveyStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */
/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
env: { account: process.env['CDK_DEFAULT_ACCOUNT'], region: process.env['CDK_DEFAULT_REGION'] },

Check failure on line 10 in bin/hydro-survey.ts

View workflow job for this annotation

GitHub Actions / deploy

Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

Check failure on line 10 in bin/hydro-survey.ts

View workflow job for this annotation

GitHub Actions / deploy

Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
});
43 changes: 33 additions & 10 deletions lib/hydro-survey-stack.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import * as cdk from 'aws-cdk-lib';
import { Duration, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
import { Bucket, StorageClass } from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
// import * as sqs from 'aws-cdk-lib/aws-sqs';

export class HydroSurveyStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
export const surveyLandingZoneBucketLogicalId = 'SurveyLandingZone';
export const surveyProcessingBucketLogicalId = 'SurveyProcessing';
export const surveyConsumptionBucketLogicalId = 'SurveyConsumption';

// The code that defines your stack goes here
export class HydroSurveyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

// example resource
// const queue = new sqs.Queue(this, 'HydroSurveyQueue', {
// visibilityTimeout: cdk.Duration.seconds(300)
// });
new Bucket(this, surveyLandingZoneBucketLogicalId, {
removalPolicy: RemovalPolicy.RETAIN,
});
new Bucket(this, surveyProcessingBucketLogicalId, {
removalPolicy: RemovalPolicy.RETAIN,
versioned: true,
});
new Bucket(this, surveyConsumptionBucketLogicalId, {
removalPolicy: RemovalPolicy.RETAIN,
lifecycleRules: [
{
transitions: [
{
storageClass: StorageClass.INFREQUENT_ACCESS,
transitionAfter: Duration.days(365),
},
{
storageClass: StorageClass.GLACIER,
transitionAfter: Duration.days(8 * 365),
},
],
},
],
versioned: true,
});
}
}
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
"source-map-support": "^0.5.21"
},
"devDependencies": {
"@types/node": "^20.6.2",
"ts-node": "^10.9.1",
"typescript": "~5.2.2"
"ts-node": "^10.9.1"
},
"bin": {
"hydro-aws-infrastructure": "bin/hydro-aws-infrastructure.js"
"hydro-survey": "bin/hydro-survey.js"
},
"scripts": {
"build": "tsc",
Expand Down
114 changes: 110 additions & 4 deletions test/hydro-survey-stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,85 @@
import { test } from 'node:test';

import { App } from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import { CfnBucket } from 'aws-cdk-lib/aws-s3';

import { HydroSurveyStack } from '../lib/hydro-survey-stack';
import {
HydroSurveyStack,
surveyConsumptionBucketLogicalId,
surveyLandingZoneBucketLogicalId,
surveyProcessingBucketLogicalId,
} from '../lib/hydro-survey-stack';

test('Should be able to instantiate stack', () => {
const app = new App();
new HydroSurveyStack(app, anyStackId());
const app = new App();

const stack = new HydroSurveyStack(app, anyStackId(), {
env: { account: anyAwsAccountId(), region: anyAwsRegionName() },
});

const template = Template.fromStack(stack);

test('Template configuration', () => {
template.templateMatches({
Resources: {
[getLogicalId(surveyLandingZoneBucketLogicalId)]: {
DeletionPolicy: 'Retain',
UpdateReplacePolicy: 'Retain',
},
[getLogicalId(surveyProcessingBucketLogicalId)]: {
DeletionPolicy: 'Retain',
UpdateReplacePolicy: 'Retain',
Properties: {
VersioningConfiguration: {
Status: 'Enabled',
},
},
},
[getLogicalId(surveyConsumptionBucketLogicalId)]: {
DeletionPolicy: 'Retain',
UpdateReplacePolicy: 'Retain',
Properties: {
LifecycleConfiguration: {
Rules: [
{
Status: 'Enabled',
Transitions: [
{
StorageClass: 'STANDARD_IA',
TransitionInDays: 365,
},
{
StorageClass: 'GLACIER',
TransitionInDays: 8 * 365,
},
],
},
],
},
VersioningConfiguration: {
Status: 'Enabled',
},
},
},
},
});
});

function getLogicalId(constructId: string): string {
return stack.getLogicalId(stack.node.tryFindChild(constructId)?.node.defaultChild as CfnBucket);
}

function anyAwsAccountId(): string {
// 12 digits, starting with a non-zero digit
const digits = [1 + anyNonNegativeInteger(9)];

for (let counter = 0; counter < 11; counter++) {
digits.push(anyNonNegativeInteger(10));
}

return digits.join('');
}

function anyStackId(): string {
// 1 to 64 alphanumeric characters, starting with an alpha character
const alphas = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
Expand All @@ -22,6 +93,41 @@ function anyStackId(): string {
return result;
}

function anyAwsRegionName(): string {
const allRegions = [
'af-south-1',
'ap-east-1',
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-south-1',
'ap-south-2',
'ap-southeast-1',
'ap-southeast-2',
'ap-southeast-3',
'ap-southeast-4',
'ca-central-1',
'eu-central-1',
'eu-central-2',
'eu-north-1',
'eu-south-1',
'eu-south-2',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'il-central-1',
'me-central-1',
'me-south-1',
'sa-east-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
];
const randomIndex = anyNonNegativeInteger(allRegions.length);
return allRegions[randomIndex]!;
}

function anyNonNegativeInteger(stop: number): number {
return Math.floor(Math.random() * stop);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"strictPropertyInitialization": false,
"typeRoots": ["./node_modules/@types"]
},
"exclude": ["node_modules", "cdk.out"]
"exclude": ["cdk.out", "node_modules"]
}

0 comments on commit bf4d82e

Please sign in to comment.