@goldstack/utils-terraform-aws 0.3.72 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- import AWS from 'aws-sdk';
2
- export declare const createState: (params: {
3
- credentials: AWS.Credentials;
4
- dynamoDBTableName: string;
5
- bucketName: string;
6
- }) => Promise<void>;
1
+ import AWS from 'aws-sdk';
2
+ export declare const createState: (params: {
3
+ credentials: AWS.Credentials;
4
+ dynamoDBTableName: string;
5
+ bucketName: string;
6
+ }) => Promise<void>;
7
7
  //# sourceMappingURL=tfState.d.ts.map
@@ -1,60 +1,60 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createState = void 0;
7
- const aws_sdk_1 = __importDefault(require("aws-sdk"));
8
- const assertDynamoDBTable = async (params) => {
9
- // defining a table as required by Terraform https://www.terraform.io/docs/language/settings/backends/s3.html#dynamodb_table
10
- const tableDef = {
11
- AttributeDefinitions: [
12
- {
13
- AttributeName: 'LockID',
14
- AttributeType: 'S',
15
- },
16
- ],
17
- KeySchema: [
18
- {
19
- AttributeName: 'LockID',
20
- KeyType: 'HASH',
21
- },
22
- ],
23
- TableName: params.tableName,
24
- BillingMode: 'PAY_PER_REQUEST',
25
- };
26
- try {
27
- await params.dynamoDB.createTable(tableDef).promise();
28
- }
29
- catch (e) {
30
- const error = e;
31
- // if there is a resource in use exception, ignore it.
32
- if (error && error.code !== 'ResourceInUseException') {
33
- throw new Error(error.message);
34
- }
35
- }
36
- };
37
- const assertS3Bucket = async (params) => {
38
- const bucketParams = {
39
- Bucket: params.bucketName,
40
- };
41
- try {
42
- console.log('Accessing/creating bucket for Terraform state', bucketParams.Bucket);
43
- await params.s3.createBucket(bucketParams).promise();
44
- }
45
- catch (error) {
46
- // if bucket already exists, ignore error
47
- if (error && error.code !== 'BucketAlreadyOwnedByYou') {
48
- console.error('Cannot create bucket ', params.bucketName, ' error code', error.code);
49
- throw new Error('Cannot create S3 state bucket: ' + error.message);
50
- }
51
- }
52
- };
53
- const createState = async (params) => {
54
- const dynamoDB = new aws_sdk_1.default.DynamoDB({ apiVersion: '2012-08-10' });
55
- await assertDynamoDBTable({ dynamoDB, tableName: params.dynamoDBTableName });
56
- const s3 = new aws_sdk_1.default.S3({ apiVersion: '2006-03-01' });
57
- await assertS3Bucket({ s3, bucketName: params.bucketName });
58
- };
59
- exports.createState = createState;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createState = void 0;
7
+ const aws_sdk_1 = __importDefault(require("aws-sdk"));
8
+ const assertDynamoDBTable = async (params) => {
9
+ // defining a table as required by Terraform https://www.terraform.io/docs/language/settings/backends/s3.html#dynamodb_table
10
+ const tableDef = {
11
+ AttributeDefinitions: [
12
+ {
13
+ AttributeName: 'LockID',
14
+ AttributeType: 'S',
15
+ },
16
+ ],
17
+ KeySchema: [
18
+ {
19
+ AttributeName: 'LockID',
20
+ KeyType: 'HASH',
21
+ },
22
+ ],
23
+ TableName: params.tableName,
24
+ BillingMode: 'PAY_PER_REQUEST',
25
+ };
26
+ try {
27
+ await params.dynamoDB.createTable(tableDef).promise();
28
+ }
29
+ catch (e) {
30
+ const error = e;
31
+ // if there is a resource in use exception, ignore it.
32
+ if (error && error.code !== 'ResourceInUseException') {
33
+ throw new Error(error.message);
34
+ }
35
+ }
36
+ };
37
+ const assertS3Bucket = async (params) => {
38
+ const bucketParams = {
39
+ Bucket: params.bucketName,
40
+ };
41
+ try {
42
+ console.log('Accessing/creating bucket for Terraform state', bucketParams.Bucket);
43
+ await params.s3.createBucket(bucketParams).promise();
44
+ }
45
+ catch (error) {
46
+ // if bucket already exists, ignore error
47
+ if (error && error.code !== 'BucketAlreadyOwnedByYou') {
48
+ console.error('Cannot create bucket ', params.bucketName, ' error code', error.code);
49
+ throw new Error('Cannot create S3 state bucket: ' + error.message);
50
+ }
51
+ }
52
+ };
53
+ const createState = async (params) => {
54
+ const dynamoDB = new aws_sdk_1.default.DynamoDB({ apiVersion: '2012-08-10' });
55
+ await assertDynamoDBTable({ dynamoDB, tableName: params.dynamoDBTableName });
56
+ const s3 = new aws_sdk_1.default.S3({ apiVersion: '2006-03-01' });
57
+ await assertS3Bucket({ s3, bucketName: params.bucketName });
58
+ };
59
+ exports.createState = createState;
60
60
  //# sourceMappingURL=tfState.js.map
@@ -1,13 +1,16 @@
1
- import { AWSTerraformState } from '@goldstack/infra-aws';
2
- import { CloudProvider, TerraformDeployment, TerraformOptions } from '@goldstack/utils-terraform';
3
- import AWS from 'aws-sdk';
4
- export declare class AWSCloudProvider implements CloudProvider {
5
- user: AWS.Credentials;
6
- remoteStateConfig: AWSTerraformState;
7
- generateEnvVariableString: () => string;
8
- setEnvVariables: () => void;
9
- getTfStateVariables: (deployment: TerraformDeployment) => [string, string][];
10
- constructor(credentials: AWS.Credentials, awsConfig: AWSTerraformState);
11
- }
12
- export declare const terraformAwsCli: (args: string[], options?: TerraformOptions) => Promise<void>;
1
+ import { AWSTerraformState } from '@goldstack/infra-aws';
2
+ import { CloudProvider, TerraformDeployment, TerraformOptions } from '@goldstack/utils-terraform';
3
+ import AWS from 'aws-sdk';
4
+ export declare class AWSCloudProvider implements CloudProvider {
5
+ user: AWS.Credentials;
6
+ remoteStateConfig: AWSTerraformState;
7
+ generateEnvVariableString: () => string;
8
+ setEnvVariables: () => void;
9
+ getTfStateVariables: (deployment: TerraformDeployment) => [
10
+ string,
11
+ string
12
+ ][];
13
+ constructor(credentials: AWS.Credentials, awsConfig: AWSTerraformState);
14
+ }
15
+ export declare const terraformAwsCli: (args: string[], options?: TerraformOptions) => Promise<void>;
13
16
  //# sourceMappingURL=utilsTerraformAws.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utilsTerraformAws.d.ts","sourceRoot":"","sources":["../../src/utilsTerraformAws.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,iBAAiB,EAElB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAEL,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EACjB,MAAM,4BAA4B,CAAC;AACpC,OAAO,GAAG,MAAM,SAAS,CAAC;AAiB1B,qBAAa,gBAAiB,YAAW,aAAa;IACpD,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC;IACtB,iBAAiB,EAAE,iBAAiB,CAAC;IAErC,yBAAyB,QAAO,MAAM,CAOpC;IAEF,eAAe,QAAO,IAAI,CAKxB;IAEF,mBAAmB,eACL,mBAAmB,KAC9B,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAuCnB;gBAEU,WAAW,EAAE,GAAG,CAAC,WAAW,EAAE,SAAS,EAAE,iBAAiB;CAIvE;AAED,eAAO,MAAM,eAAe,SACpB,MAAM,EAAE,YACJ,gBAAgB,KACzB,QAAQ,IAAI,CAkCd,CAAC"}
1
+ {"version":3,"file":"utilsTerraformAws.d.ts","sourceRoot":"","sources":["../../src/utilsTerraformAws.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,iBAAiB,EAElB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAEL,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EACjB,MAAM,4BAA4B,CAAC;AACpC,OAAO,GAAG,MAAM,SAAS,CAAC;AAiB1B,qBAAa,gBAAiB,YAAW,aAAa;IACpD,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC;IACtB,iBAAiB,EAAE,iBAAiB,CAAC;IAErC,yBAAyB,QAAO,MAAM,CAOpC;IAEF,eAAe,QAAO,IAAI,CAKxB;IAEF,mBAAmB,eACL,mBAAmB,KAC9B;QAAC,MAAM;QAAE,MAAM;KAAC,EAAE,CAuCnB;gBAEU,WAAW,EAAE,GAAG,CAAC,WAAW,EAAE,SAAS,EAAE,iBAAiB;CAIvE;AAED,eAAO,MAAM,eAAe,SACpB,MAAM,EAAE,YACJ,gBAAgB,KACzB,QAAQ,IAAI,CAkCd,CAAC"}
@@ -1,88 +1,88 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.terraformAwsCli = exports.AWSCloudProvider = void 0;
7
- const infra_aws_1 = require("@goldstack/infra-aws");
8
- const utils_terraform_1 = require("@goldstack/utils-terraform");
9
- const aws_sdk_1 = __importDefault(require("aws-sdk"));
10
- const tfState_1 = require("./tfState");
11
- const crypto_1 = __importDefault(require("crypto"));
12
- const getRemoteStateConfig = (config, userName) => {
13
- const userConfig = config.remoteState.filter((u) => u.user === userName);
14
- if (userConfig.length === 0) {
15
- throw new Error(`Cannot find aws user ${userName} in project terraform config`);
16
- }
17
- return userConfig[0];
18
- };
19
- class AWSCloudProvider {
20
- constructor(credentials, awsConfig) {
21
- this.generateEnvVariableString = () => {
22
- return (`-e AWS_ACCESS_KEY_ID=${this.user.accessKeyId} ` +
23
- `-e AWS_SECRET_ACCESS_KEY=${this.user.secretAccessKey} ` +
24
- `-e AWS_SESSION_TOKEN=${this.user.sessionToken || ''} ` +
25
- `-e AWS_DEFAULT_REGION=${aws_sdk_1.default.config.region} `);
26
- };
27
- this.setEnvVariables = () => {
28
- process.env.AWS_ACCESS_KEY_ID = this.user.accessKeyId;
29
- process.env.AWS_SECRET_ACCESS_KEY = this.user.secretAccessKey;
30
- process.env.AWS_DEFAULT_REGION = aws_sdk_1.default.config.region;
31
- process.env.AWS_SESSION_TOKEN = this.user.sessionToken || '';
32
- };
33
- this.getTfStateVariables = (deployment) => {
34
- const remoteStateConfig = getRemoteStateConfig(this.remoteStateConfig, deployment.awsUser);
35
- const bucket = remoteStateConfig.terraformStateBucket;
36
- if (!bucket) {
37
- throw new Error(`TF state bucket not defined for user ${deployment.awsUser}`);
38
- }
39
- const ddTable = remoteStateConfig.terraformStateDynamoDBTable;
40
- if (!ddTable) {
41
- throw new Error(`TF state DynamoDB table not defined for user ${deployment.awsUser}`);
42
- }
43
- if (!aws_sdk_1.default.config.region) {
44
- throw new Error('AWS region not defined');
45
- }
46
- const tfKey = deployment.tfStateKey;
47
- if (!tfKey) {
48
- throw new Error('Terraform state key not defined');
49
- }
50
- return [
51
- ['bucket', bucket],
52
- ['key', `${tfKey}`],
53
- ['region', aws_sdk_1.default.config.region],
54
- ['dynamodb_table', ddTable],
55
- ];
56
- };
57
- this.user = credentials;
58
- this.remoteStateConfig = awsConfig;
59
- }
60
- }
61
- exports.AWSCloudProvider = AWSCloudProvider;
62
- const terraformAwsCli = async (args, options) => {
63
- const deploymentName = args[1];
64
- const deployment = (0, infra_aws_1.readDeploymentFromPackageConfig)(deploymentName);
65
- const credentials = await (0, infra_aws_1.getAWSUser)(deployment.awsUser);
66
- const awsTerraformConfig = (0, infra_aws_1.assertTerraformConfig)(deployment.awsUser);
67
- const projectHash = crypto_1.default.randomBytes(20).toString('hex');
68
- const remoteStateConfig = getRemoteStateConfig(awsTerraformConfig, deployment.awsUser);
69
- if (!remoteStateConfig.terraformStateBucket) {
70
- remoteStateConfig.terraformStateBucket = `goldstack-tfstate-${projectHash}`;
71
- (0, infra_aws_1.writeTerraformConfig)(awsTerraformConfig);
72
- }
73
- if (!remoteStateConfig.terraformStateDynamoDBTable) {
74
- remoteStateConfig.terraformStateDynamoDBTable = `goldstack-tfstate-${projectHash}-lock`;
75
- (0, infra_aws_1.writeTerraformConfig)(awsTerraformConfig);
76
- }
77
- await (0, tfState_1.createState)({
78
- bucketName: remoteStateConfig.terraformStateBucket,
79
- dynamoDBTableName: remoteStateConfig.terraformStateDynamoDBTable,
80
- credentials,
81
- });
82
- (0, utils_terraform_1.terraformCli)(args, {
83
- ...options,
84
- provider: new AWSCloudProvider(credentials, awsTerraformConfig),
85
- });
86
- };
87
- exports.terraformAwsCli = terraformAwsCli;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.terraformAwsCli = exports.AWSCloudProvider = void 0;
7
+ const infra_aws_1 = require("@goldstack/infra-aws");
8
+ const utils_terraform_1 = require("@goldstack/utils-terraform");
9
+ const aws_sdk_1 = __importDefault(require("aws-sdk"));
10
+ const tfState_1 = require("./tfState");
11
+ const crypto_1 = __importDefault(require("crypto"));
12
+ const getRemoteStateConfig = (config, userName) => {
13
+ const userConfig = config.remoteState.filter((u) => u.user === userName);
14
+ if (userConfig.length === 0) {
15
+ throw new Error(`Cannot find aws user ${userName} in project terraform config`);
16
+ }
17
+ return userConfig[0];
18
+ };
19
+ class AWSCloudProvider {
20
+ constructor(credentials, awsConfig) {
21
+ this.generateEnvVariableString = () => {
22
+ return (`-e AWS_ACCESS_KEY_ID=${this.user.accessKeyId} ` +
23
+ `-e AWS_SECRET_ACCESS_KEY=${this.user.secretAccessKey} ` +
24
+ `-e AWS_SESSION_TOKEN=${this.user.sessionToken || ''} ` +
25
+ `-e AWS_DEFAULT_REGION=${aws_sdk_1.default.config.region} `);
26
+ };
27
+ this.setEnvVariables = () => {
28
+ process.env.AWS_ACCESS_KEY_ID = this.user.accessKeyId;
29
+ process.env.AWS_SECRET_ACCESS_KEY = this.user.secretAccessKey;
30
+ process.env.AWS_DEFAULT_REGION = aws_sdk_1.default.config.region;
31
+ process.env.AWS_SESSION_TOKEN = this.user.sessionToken || '';
32
+ };
33
+ this.getTfStateVariables = (deployment) => {
34
+ const remoteStateConfig = getRemoteStateConfig(this.remoteStateConfig, deployment.awsUser);
35
+ const bucket = remoteStateConfig.terraformStateBucket;
36
+ if (!bucket) {
37
+ throw new Error(`TF state bucket not defined for user ${deployment.awsUser}`);
38
+ }
39
+ const ddTable = remoteStateConfig.terraformStateDynamoDBTable;
40
+ if (!ddTable) {
41
+ throw new Error(`TF state DynamoDB table not defined for user ${deployment.awsUser}`);
42
+ }
43
+ if (!aws_sdk_1.default.config.region) {
44
+ throw new Error('AWS region not defined');
45
+ }
46
+ const tfKey = deployment.tfStateKey;
47
+ if (!tfKey) {
48
+ throw new Error('Terraform state key not defined');
49
+ }
50
+ return [
51
+ ['bucket', bucket],
52
+ ['key', `${tfKey}`],
53
+ ['region', aws_sdk_1.default.config.region],
54
+ ['dynamodb_table', ddTable],
55
+ ];
56
+ };
57
+ this.user = credentials;
58
+ this.remoteStateConfig = awsConfig;
59
+ }
60
+ }
61
+ exports.AWSCloudProvider = AWSCloudProvider;
62
+ const terraformAwsCli = async (args, options) => {
63
+ const deploymentName = args[1];
64
+ const deployment = (0, infra_aws_1.readDeploymentFromPackageConfig)(deploymentName);
65
+ const credentials = await (0, infra_aws_1.getAWSUser)(deployment.awsUser);
66
+ const awsTerraformConfig = (0, infra_aws_1.assertTerraformConfig)(deployment.awsUser);
67
+ const projectHash = crypto_1.default.randomBytes(20).toString('hex');
68
+ const remoteStateConfig = getRemoteStateConfig(awsTerraformConfig, deployment.awsUser);
69
+ if (!remoteStateConfig.terraformStateBucket) {
70
+ remoteStateConfig.terraformStateBucket = `goldstack-tfstate-${projectHash}`;
71
+ (0, infra_aws_1.writeTerraformConfig)(awsTerraformConfig);
72
+ }
73
+ if (!remoteStateConfig.terraformStateDynamoDBTable) {
74
+ remoteStateConfig.terraformStateDynamoDBTable = `goldstack-tfstate-${projectHash}-lock`;
75
+ (0, infra_aws_1.writeTerraformConfig)(awsTerraformConfig);
76
+ }
77
+ await (0, tfState_1.createState)({
78
+ bucketName: remoteStateConfig.terraformStateBucket,
79
+ dynamoDBTableName: remoteStateConfig.terraformStateDynamoDBTable,
80
+ credentials,
81
+ });
82
+ (0, utils_terraform_1.terraformCli)(args, {
83
+ ...options,
84
+ provider: new AWSCloudProvider(credentials, awsTerraformConfig),
85
+ });
86
+ };
87
+ exports.terraformAwsCli = terraformAwsCli;
88
88
  //# sourceMappingURL=utilsTerraformAws.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goldstack/utils-terraform-aws",
3
- "version": "0.3.72",
3
+ "version": "0.4.0",
4
4
  "description": "Easily deploy Terraform IaC with npm package scripts",
5
5
  "keywords": [
6
6
  "goldstack",
@@ -36,16 +36,16 @@
36
36
  "version:apply:force": "yarn version $@ && yarn version apply"
37
37
  },
38
38
  "dependencies": {
39
- "@goldstack/infra-aws": "0.3.49",
40
- "@goldstack/utils-config": "0.3.37",
41
- "@goldstack/utils-log": "0.2.17",
42
- "@goldstack/utils-package": "0.3.43",
43
- "@goldstack/utils-sh": "0.4.35",
44
- "@goldstack/utils-terraform": "0.3.73",
39
+ "@goldstack/infra-aws": "0.4.0",
40
+ "@goldstack/utils-config": "0.4.0",
41
+ "@goldstack/utils-log": "0.3.0",
42
+ "@goldstack/utils-package": "0.4.0",
43
+ "@goldstack/utils-sh": "0.5.0",
44
+ "@goldstack/utils-terraform": "0.4.0",
45
45
  "aws-sdk": "2.1055.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@goldstack/utils-git": "0.1.37",
48
+ "@goldstack/utils-git": "0.2.0",
49
49
  "@types/jest": "^28.1.8",
50
50
  "@types/node": "^18.7.13",
51
51
  "jest": "^28.1.0",