-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·123 lines (101 loc) · 2.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
import * as aws from 'aws-sdk';
import { prompt } from 'enquirer';
import * as fs from 'fs';
export class BackendTF {
private async createS3Bucket(bucketName: string, region: string) {
const s3 = new aws.S3();
const params = {
Bucket: bucketName,
ACL: 'private',
CreateBucketConfiguration: {
LocationConstraint: region
},
};
await s3.createBucket(params).promise();
const publicAccess = {
Bucket: bucketName,
PublicAccessBlockConfiguration: {
BlockPublicAcls: true,
BlockPublicPolicy: true,
IgnorePublicAcls: true,
RestrictPublicBuckets: true,
},
};
await s3.putPublicAccessBlock(publicAccess).promise();
const bucketEncryption = {
Bucket: bucketName,
ServerSideEncryptionConfiguration: {
Rules: [
{
ApplyServerSideEncryptionByDefault: {
SSEAlgorithm: 'AES256',
},
},
],
},
};
await s3.putBucketEncryption(bucketEncryption).promise();
const bucketVersioning = {
Bucket: bucketName,
VersioningConfiguration: {
MFADelete: 'Disabled',
Status: 'Enabled',
},
}
await s3.putBucketVersioning(bucketVersioning).promise();
console.info(`Bucket ${bucketName} Created.`);
}
private async createDynamoDBTable(name: string) {
const dynamodb = new aws.DynamoDB();
const params = {
TableName : name,
KeySchema: [
{ AttributeName: 'LockID', KeyType: 'HASH'},
],
AttributeDefinitions: [
{ AttributeName: 'LockID', AttributeType: 'S' },
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5,
},
};
await dynamodb.createTable(params).promise();
console.info(`DynamoDB Table ${name} is created!`)
}
public async init() {
const { repoName } = await prompt({
type: 'input',
name: 'repoName',
message: 'Name of repository or project?',
});
const { nameFileBackend } = await prompt({
type: 'input',
name: 'nameFileBackend',
message: 'Name of file <your-custom-name>.tfbackend?',
initial: 'staging',
});
const { region } = await prompt({
type: 'input',
name: 'region',
message: 'Region?',
initial: 'us-east-2',
});
aws.config.update({ region });
await this.createS3Bucket(`${repoName}-state`, region);
await this.createDynamoDBTable(`${repoName}_lock`);
// Generate <name>.tfbackend file
const backendFileContent =
`
bucket = "${repoName}_state"
key = "state.tfstate"
region = "${region}"
dynamodb_table = "${repoName}_lock"
`;
fs.writeFileSync(`${nameFileBackend}.tfbackend`, backendFileContent);
console.info(`Backend file: ${nameFileBackend}.tfbackend generated successfully`);
}
}
const backend: BackendTF = new BackendTF();
backend.init().catch((err: any) => { console.error(err); });