-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from mattzcarey/feat/buildResourceName
feat: added buildResourceName function
- Loading branch information
Showing
5 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters