Skip to content

Commit

Permalink
Merge pull request #123 from mattzcarey/feat/buildResourceName
Browse files Browse the repository at this point in the history
feat: added buildResourceName function
  • Loading branch information
lizacullis committed Aug 8, 2023
2 parents c573d55 + fc7eb9a commit 5e3f300
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 8 deletions.
22 changes: 22 additions & 0 deletions services/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# How to name and deploy your stacks

When developing be sure to use the `buildResourceName()` to define the names of your stacks and constructs.

The `buildResourceName("<resource_name>")` uses command line arguments set when deploying to prepend additional info to the resource names.

### Command line argument options

- `stage` - the stage that you are developing on, ie. `dev`, `staging`,`prod`.
- `stackName` - the unique name of the stack, so that during development multiple people can be working on the same stack.
- `region` - the region, defaults to `eu-west-1`. This is not used in the naming of resources.
### Example Deploy command

When you want to deploy you can set these cli options.

``` bash
cdk deploy --context stackName=<your_stack_name>
```

``` bash
cdk deploy --context stage=<your_stage>
```
8 changes: 4 additions & 4 deletions services/core/functions/review-lambda/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { Architecture, Runtime } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";
import { join } from "path";
import { buildResourceName } from "../../helpers/env-helpers";

const OPENAI_API_KEY_PARAM_NAME = "GLOBAL_OPENAI_API_KEY"
const OPENAI_API_KEY_PARAM_NAME = "GLOBAL_OPENAI_API_KEY";

export class ReviewLambda extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);

const lambda = new NodejsFunction(this, "review-lambda", {
functionName: "review-lambda",
const lambda = new NodejsFunction(this, buildResourceName("review-lambda"), {
entry: join(__dirname, "index.ts"),
handler: "main",
runtime: Runtime.NODEJS_18_X,
Expand All @@ -29,7 +29,7 @@ export class ReviewLambda extends Construct {
resource: "parameter",
resourceName: OPENAI_API_KEY_PARAM_NAME,
});

lambda.addToRolePolicy(
new PolicyStatement({
actions: ["ssm:GetParameter"],
Expand Down
75 changes: 75 additions & 0 deletions services/core/helpers/env-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const defaultEnvironment = "dev";
const defaultRegion = "eu-west-2";

export type GetArgProps = {
cliArg: string;
processEnvName: string;
defaultValue?: string;
};

export const getArg = ({
cliArg,
processEnvName,
defaultValue,
}: GetArgProps): string => {
const isNonEmptyString = (arg: unknown): arg is string =>
typeof arg === "string" && arg !== "";

const getCdkContext = (): Record<string, unknown> | undefined => {
const cdkContextEnv = process.env.CDK_CONTEXT_JSON;

return cdkContextEnv != null
? (JSON.parse(cdkContextEnv) as Record<string, unknown>)
: undefined;
};

const findCliArg = (key: string): string | undefined => {
const index = process.argv.findIndex((arg) => arg === `--${key}`);
return index !== -1 && index + 1 < process.argv.length
? process.argv[index + 1]
: undefined;
};

const argSources = [
findCliArg(cliArg),
getCdkContext()?.[cliArg] as string | undefined,
process.env[processEnvName],
defaultValue,
];

for (const arg of argSources) {
if (isNonEmptyString(arg)) {
return arg;
}
}

throw new Error(
`--${cliArg} CLI argument or ${processEnvName} env var required.`
);
};

export const buildResourceName = (resourceName: string): string =>
`${getStackName()}-${resourceName}`;

export const getStackName = (): string => {
const arg = getArg({
cliArg: "stackName",
processEnvName: "STACK_NAME",
defaultValue: getStage(),
});
return arg;
};
export const getStage = (): string => {
return getArg({
cliArg: "stage",
processEnvName: "STAGE",
defaultValue: defaultEnvironment,
});
};

export const getRegion = (): string =>
getArg({
cliArg: 'region',
processEnvName: 'REGION',
defaultValue: defaultRegion,
});
11 changes: 8 additions & 3 deletions services/core/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
import "source-map-support/register";
import * as cdk from "aws-cdk-lib";
import { CoreStack } from "./stacks/core-stack";
import { buildResourceName, getStage, getRegion } from "../helpers/env-helpers";

const app = new cdk.App();
new CoreStack(app, `crgpt-core-stack`, {
// stage,
// env: { region },
const stage = getStage();
const region = getRegion();

new CoreStack(app, `${stage}-crgpt-core-stack`, {
stackName: buildResourceName("crgpt-core-stack"),
stage: stage,
env: { region },
});
5 changes: 4 additions & 1 deletion services/core/resources/stacks/core-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { ReviewLambda } from "../../functions/review-lambda/config";

interface CoreStackProps extends StackProps {
stage: string,
}
export class CoreStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps) {
constructor(scope: Construct, id: string, props: CoreStackProps) {
super(scope, id, props);
new ReviewLambda(this, "review-lambda-construct");
}
Expand Down

0 comments on commit 5e3f300

Please sign in to comment.