Skip to content

Commit d3dc955

Browse files
authored
Fix Issue #1126 - the race condition of instance role and eb environment (#1152)
* created an instance role for eb to fix issue #1126 * changed solution to node.js 20
1 parent 3edf628 commit d3dc955

File tree

5 files changed

+84
-29
lines changed

5 files changed

+84
-29
lines changed

typescript/elasticbeanstalk/elasticbeanstalk-environment/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ cdk synth
3030
cdk bootstrap
3131
cdk deploy
3232
33-
```
33+
```
34+
35+
#### How to retrieve platform or solutionStackName?
36+
37+
- `platform`
38+
```shell
39+
aws elasticbeanstalk list-platform-versions \
40+
--query 'PlatformSummaryList[*].[PlatformArn,PlatformBranchName]' --output table
41+
```
42+
43+
- `solutionStackName`
44+
```shell
45+
aws elasticbeanstalk list-available-solution-stacks \
46+
--query 'SolutionStackDetails[*].[SolutionStackName]' --output table
47+
```
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"context": {
3-
"platform": "arn:aws:elasticbeanstalk:us-east-1::platform/Tomcat 8 with Java 8 running on 64bit Amazon Linux"
3+
"platform": "arn:aws:elasticbeanstalk:us-east-1::platform/Corretto 21 running on 64bit Amazon Linux 2023/4.5.0",
4+
"solution": "64bit Amazon Linux 2023 v6.5.0 running Node.js 20"
45
},
56
"app": "node index"
67
}

typescript/elasticbeanstalk/elasticbeanstalk-environment/index.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22
import * as cdk from 'aws-cdk-lib';
3-
import * as elasticbeanstalk from 'aws-cdk-lib/aws-elasticbeanstalk';
3+
import { CfnApplication, CfnEnvironment } from 'aws-cdk-lib/aws-elasticbeanstalk';
4+
import { InstanceProfile, ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
45

56

67
export class CdkStack extends cdk.Stack {
@@ -11,21 +12,57 @@ export class CdkStack extends cdk.Stack {
1112
const node = this.node;
1213

1314
const appName = 'MyApp';
14-
1515
const platform = node.tryGetContext("platform");
16+
const solution = node.tryGetContext("solution");
17+
18+
19+
// Create Role:
20+
const ebRole = new Role(this, `${appName}-eb-role` , {
21+
assumedBy: new ServicePrincipal('ec2.amazonaws.com'),
22+
roleName:`${appName}-eb-role`
23+
});
24+
25+
// some managed policies eb must have
26+
ebRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWebTier'));
27+
ebRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkMulticontainerDocker'));
28+
ebRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWorkerTier'));
29+
30+
//Custom policies
31+
//access to config secrets
32+
33+
const roleARN = ebRole.roleArn;
34+
35+
// Create instance profile
36+
const instanceProfile = new InstanceProfile(this, `${appName}-instance-role`, {
37+
role: ebRole,
38+
instanceProfileName: `${appName}-instance-role`,
39+
})
1640

17-
const app = new elasticbeanstalk.CfnApplication(this, 'Application', {
41+
const app = new CfnApplication(this, `${appName}-Application`, {
1842
applicationName: appName
1943
});
2044

21-
const env = new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
22-
environmentName: 'MySampleEnvironment',
23-
applicationName: app.applicationName || appName,
24-
platformArn: platform
45+
const env = new CfnEnvironment(this, `${appName}-Environment`, {
46+
environmentName: `${appName}-Environment`,
47+
applicationName: appName,
48+
solutionStackName: solution,
49+
//platformArn: platform,
50+
optionSettings: [
51+
{
52+
namespace: "aws:autoscaling:launchconfiguration",
53+
optionName: "IamInstanceProfile",
54+
value: instanceProfile.instanceProfileArn,
55+
},
56+
{
57+
namespace: "aws:elasticbeanstalk:environment",
58+
optionName: "EnvironmentType",
59+
value: "SingleInstance",
60+
},
61+
]
2562
});
2663

2764
// to ensure the application is created before the environment
28-
env.addDependsOn(app);
65+
env.addDependency(app);
2966
}
3067
}
3168

typescript/elasticbeanstalk/elasticbeanstalk-environment/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"aws-cdk": "2.1004.0"
2121
},
2222
"dependencies": {
23-
"aws-cdk-lib": "2.185.0",
23+
"aws-cdk-lib": "2.188.0",
2424
"constructs": "^10.0.0",
2525
"source-map-support": "^0.5.9"
2626
}
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
{
22
"compilerOptions": {
3-
"target":"ES2018",
4-
"module": "commonjs",
5-
"lib": ["es2016", "es2017.object", "es2017.string"],
6-
"strict": true,
7-
"noImplicitAny": true,
8-
"strictNullChecks": true,
9-
"noImplicitThis": true,
10-
"alwaysStrict": true,
11-
"noUnusedLocals": true,
12-
"noUnusedParameters": true,
13-
"noImplicitReturns": true,
14-
"noFallthroughCasesInSwitch": false,
15-
"inlineSourceMap": true,
16-
"inlineSources": true,
17-
"experimentalDecorators": true,
18-
"strictPropertyInitialization":false
19-
}
20-
}
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"lib": ["es2020"],
6+
"declaration": true,
7+
"strict": true,
8+
"noImplicitAny": true,
9+
"strictNullChecks": true,
10+
"noImplicitThis": true,
11+
"alwaysStrict": true,
12+
"noUnusedLocals": false,
13+
"noUnusedParameters": false,
14+
"noImplicitReturns": true,
15+
"noFallthroughCasesInSwitch": false,
16+
"inlineSourceMap": true,
17+
"inlineSources": true,
18+
"experimentalDecorators": true,
19+
"strictPropertyInitialization": false,
20+
"typeRoots": ["./node_modules/@types"]
21+
},
22+
"exclude": ["node_modules", "cdk.out"]
23+
}

0 commit comments

Comments
 (0)