-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
integ.configuration.ts
232 lines (214 loc) · 7.22 KB
/
integ.configuration.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import { App, Duration, Stack, RemovalPolicy, Fn, SecretValue } from 'aws-cdk-lib';
import { Artifact, Pipeline } from 'aws-cdk-lib/aws-codepipeline';
import { S3DeployAction, S3SourceAction } from 'aws-cdk-lib/aws-codepipeline-actions';
import { Key } from 'aws-cdk-lib/aws-kms';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import * as s3Deployment from 'aws-cdk-lib/aws-s3-deployment';
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
import { CfnDocument, StringParameter } from 'aws-cdk-lib/aws-ssm';
import {
Application,
ConfigurationContent,
ConfigurationSource,
DeploymentStrategy,
HostedConfiguration,
JsonSchemaValidator,
LambdaValidator,
RolloutStrategy,
SourcedConfiguration,
} from 'aws-cdk-lib/aws-appconfig';
const SCHEMA_STR =
`{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string"
}`;
const LAMBDA_CODE =
`
def handler(event, context):
print('This is my dummy validator')
`;
const app = new App({
postCliContext: {
'@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2': false,
},
});
const stack = new Stack(app, 'aws-appconfig-configuration');
// create application for config profile
const appConfigApp = new Application(stack, 'MyAppConfig', {
applicationName: 'AppForConfigTest',
});
const deploymentStrategy = new DeploymentStrategy(stack, 'MyDeployStrategy', {
rolloutStrategy: RolloutStrategy.linear({
growthFactor: 100,
deploymentDuration: Duration.minutes(0),
}),
});
// hosted config from file
new HostedConfiguration(stack, 'MyHostedConfigFromFile', {
application: appConfigApp,
content: ConfigurationContent.fromFile('config.json'),
});
// create basic config profile and add config version
const hostedEnv = appConfigApp.addEnvironment('HostedEnv');
new HostedConfiguration(stack, 'MyHostedConfig', {
application: appConfigApp,
content: ConfigurationContent.fromInlineText('This is my configuration content.'),
deployTo: [hostedEnv],
validators: [
JsonSchemaValidator.fromInline(SCHEMA_STR),
JsonSchemaValidator.fromFile('schema.json'),
],
deploymentStrategy,
});
// create basic config profile from add config version from file
const hostedEnvFromJson = appConfigApp.addEnvironment('HostedEnvFromJson');
new HostedConfiguration(stack, 'MyHostedConfigFromJson', {
application: appConfigApp,
content: ConfigurationContent.fromInlineText('This is the configuration content'),
deployTo: [hostedEnvFromJson],
deploymentStrategy,
});
const hostedEnvFromYaml = appConfigApp.addEnvironment('HostedEnvFromYaml');
new HostedConfiguration(stack, 'MyHostedConfigFromYaml', {
application: appConfigApp,
content: ConfigurationContent.fromInlineYaml('This is the configuration content'),
deployTo: [hostedEnvFromYaml],
deploymentStrategy,
});
// ssm paramter as configuration source
const func = new Function(stack, 'MyValidatorFunction', {
runtime: Runtime.PYTHON_3_8,
handler: 'index.handler',
code: Code.fromInline(LAMBDA_CODE),
});
const parameterEnv = appConfigApp.addEnvironment('ParameterEnv');
const ssmParameter = new StringParameter(stack, 'MyParameter', {
stringValue: 'This is the content stored in ssm parameter',
});
new SourcedConfiguration(stack, 'MyConfigFromParameter', {
name: 'TestConfigProfileStoredAsParamater',
application: appConfigApp,
description: 'This is a configuration profile used for integ testing',
location: ConfigurationSource.fromParameter(ssmParameter),
versionNumber: '1',
deployTo: [parameterEnv],
validators: [
LambdaValidator.fromFunction(func),
],
deploymentStrategy,
});
// ssm document as configuration source
const documentEnv = appConfigApp.addEnvironment('DocumentEnv');
const ssmDocument = new CfnDocument(stack, 'MyDocument', {
content: {
schemaVersion: '2.2',
description: 'Sample SSM Document',
mainSteps: [
{
action: 'aws:runShellScript',
name: 'step1',
inputs: {
runCommand: [
'echo "Hello, World!"',
],
},
},
],
},
documentType: 'Command',
name: 'TestDocument',
});
new SourcedConfiguration(stack, 'MyConfigFromDocument', {
application: appConfigApp,
location: ConfigurationSource.fromCfnDocument(ssmDocument),
versionNumber: '1',
deployTo: [documentEnv],
deploymentStrategy,
});
// S3 as configuration source
const bucketEnv = appConfigApp.addEnvironment('BucketEnv');
const bucket = new Bucket(stack, 'MyBucket', {
versioned: true,
removalPolicy: RemovalPolicy.DESTROY,
});
bucket.applyRemovalPolicy(RemovalPolicy.DESTROY);
const deployment = new s3Deployment.BucketDeployment(stack, 'DeployConfigInBucket', {
sources: [s3Deployment.Source.data('hello/world/file.txt', 'This is the content stored in S3')],
destinationBucket: bucket,
retainOnDelete: false,
});
new SourcedConfiguration(stack, 'MyConfigFromBucket', {
application: appConfigApp,
location: ConfigurationSource.fromBucket(bucket, 'hello/world/file.txt'),
description: `Sourced from ${Fn.select(0, deployment.objectKeys)}`,
deployTo: [bucketEnv],
});
// secrets manager as configuration source (without key)
const secretEnv = appConfigApp.addEnvironment('SecretEnv');
const secret = new Secret(stack, 'MySecret', {
secretStringValue: SecretValue.unsafePlainText('This is the content stored in secrets manager'),
});
new SourcedConfiguration(stack, 'MyConfigFromSecret', {
application: appConfigApp,
location: ConfigurationSource.fromSecret(secret),
deployTo: [secretEnv],
});
// secrets manager as configuration source (with key)
const secretWithKeyEnv = appConfigApp.addEnvironment('SecretEnvWithKey');
const key = new Key(stack, 'MyKey');
const secretWithKey = new Secret(stack, 'MySecretWithKey', {
secretStringValue: SecretValue.unsafePlainText('This is the content stored in secrets manager'),
encryptionKey: key,
});
new SourcedConfiguration(stack, 'MyConfigFromSecretWithKey', {
location: ConfigurationSource.fromSecret(secretWithKey),
deploymentKey: key,
application: appConfigApp,
deployTo: [secretWithKeyEnv],
});
// code pipeline as configuration source
deployment.addSource(s3Deployment.Source.data('hello/world/codepipeline.txt', 'This is the content stored in code pipeline'));
const sourceAction = new S3SourceAction({
actionName: 'Source',
bucket: bucket,
bucketKey: 'hello/world/codepipeline.txt',
output: new Artifact('SourceOutput'),
});
const deployAction = new S3DeployAction({
actionName: 'Deploy',
input: Artifact.artifact('SourceOutput'),
bucket: bucket,
extract: true,
});
const pipeline = new Pipeline(stack, 'MyPipeline', {
crossAccountKeys: true,
stages: [
{
stageName: 'beta',
actions: [sourceAction],
},
{
stageName: 'prod',
actions: [deployAction],
},
],
});
new SourcedConfiguration(stack, 'MyConfigFromPipeline', {
application: appConfigApp,
location: ConfigurationSource.fromPipeline(pipeline),
});
/* resource deployment alone is sufficient because we already have the
corresponding resource handler tests to assert that resources can be
used after created */
new IntegTest(app, 'appconfig-configuration', {
testCases: [stack],
cdkCommandOptions: {
destroy: {
args: {
force: true,
},
},
},
});