diff --git a/typescript/elasticbeanstalk/elasticbeanstalk-environment/README.md b/typescript/elasticbeanstalk/elasticbeanstalk-environment/README.md index 0ae427fb15..ee621b9f4c 100644 --- a/typescript/elasticbeanstalk/elasticbeanstalk-environment/README.md +++ b/typescript/elasticbeanstalk/elasticbeanstalk-environment/README.md @@ -30,4 +30,18 @@ cdk synth cdk bootstrap cdk deploy -``` \ No newline at end of file +``` + +#### How to retrieve platform or solutionStackName? + +- `platform` +```shell +aws elasticbeanstalk list-platform-versions \ + --query 'PlatformSummaryList[*].[PlatformArn,PlatformBranchName]' --output table +``` + +- `solutionStackName` +```shell +aws elasticbeanstalk list-available-solution-stacks \ + --query 'SolutionStackDetails[*].[SolutionStackName]' --output table +``` diff --git a/typescript/elasticbeanstalk/elasticbeanstalk-environment/cdk.json b/typescript/elasticbeanstalk/elasticbeanstalk-environment/cdk.json index d471aca945..7af2f23094 100644 --- a/typescript/elasticbeanstalk/elasticbeanstalk-environment/cdk.json +++ b/typescript/elasticbeanstalk/elasticbeanstalk-environment/cdk.json @@ -1,6 +1,7 @@ { "context": { - "platform": "arn:aws:elasticbeanstalk:us-east-1::platform/Tomcat 8 with Java 8 running on 64bit Amazon Linux" + "platform": "arn:aws:elasticbeanstalk:us-east-1::platform/Corretto 21 running on 64bit Amazon Linux 2023/4.5.0", + "solution": "64bit Amazon Linux 2023 v6.5.0 running Node.js 20" }, "app": "node index" } \ No newline at end of file diff --git a/typescript/elasticbeanstalk/elasticbeanstalk-environment/index.ts b/typescript/elasticbeanstalk/elasticbeanstalk-environment/index.ts index 8f0da30159..a605517032 100644 --- a/typescript/elasticbeanstalk/elasticbeanstalk-environment/index.ts +++ b/typescript/elasticbeanstalk/elasticbeanstalk-environment/index.ts @@ -1,6 +1,7 @@ #!/usr/bin/env node import * as cdk from 'aws-cdk-lib'; -import * as elasticbeanstalk from 'aws-cdk-lib/aws-elasticbeanstalk'; +import { CfnApplication, CfnEnvironment } from 'aws-cdk-lib/aws-elasticbeanstalk'; +import { InstanceProfile, ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam'; export class CdkStack extends cdk.Stack { @@ -11,21 +12,57 @@ export class CdkStack extends cdk.Stack { const node = this.node; const appName = 'MyApp'; - const platform = node.tryGetContext("platform"); + const solution = node.tryGetContext("solution"); + + + // Create Role: + const ebRole = new Role(this, `${appName}-eb-role` , { + assumedBy: new ServicePrincipal('ec2.amazonaws.com'), + roleName:`${appName}-eb-role` + }); + + // some managed policies eb must have + ebRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWebTier')); + ebRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkMulticontainerDocker')); + ebRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWorkerTier')); + + //Custom policies + //access to config secrets + + const roleARN = ebRole.roleArn; + + // Create instance profile + const instanceProfile = new InstanceProfile(this, `${appName}-instance-role`, { + role: ebRole, + instanceProfileName: `${appName}-instance-role`, + }) - const app = new elasticbeanstalk.CfnApplication(this, 'Application', { + const app = new CfnApplication(this, `${appName}-Application`, { applicationName: appName }); - const env = new elasticbeanstalk.CfnEnvironment(this, 'Environment', { - environmentName: 'MySampleEnvironment', - applicationName: app.applicationName || appName, - platformArn: platform + const env = new CfnEnvironment(this, `${appName}-Environment`, { + environmentName: `${appName}-Environment`, + applicationName: appName, + solutionStackName: solution, + //platformArn: platform, + optionSettings: [ + { + namespace: "aws:autoscaling:launchconfiguration", + optionName: "IamInstanceProfile", + value: instanceProfile.instanceProfileArn, + }, + { + namespace: "aws:elasticbeanstalk:environment", + optionName: "EnvironmentType", + value: "SingleInstance", + }, + ] }); // to ensure the application is created before the environment - env.addDependsOn(app); + env.addDependency(app); } } diff --git a/typescript/elasticbeanstalk/elasticbeanstalk-environment/package.json b/typescript/elasticbeanstalk/elasticbeanstalk-environment/package.json index 08106a0aff..f7dc5feedf 100644 --- a/typescript/elasticbeanstalk/elasticbeanstalk-environment/package.json +++ b/typescript/elasticbeanstalk/elasticbeanstalk-environment/package.json @@ -20,7 +20,7 @@ "aws-cdk": "2.1004.0" }, "dependencies": { - "aws-cdk-lib": "2.185.0", + "aws-cdk-lib": "2.188.0", "constructs": "^10.0.0", "source-map-support": "^0.5.9" } diff --git a/typescript/elasticbeanstalk/elasticbeanstalk-environment/tsconfig.json b/typescript/elasticbeanstalk/elasticbeanstalk-environment/tsconfig.json index 544b446da1..507608a99f 100644 --- a/typescript/elasticbeanstalk/elasticbeanstalk-environment/tsconfig.json +++ b/typescript/elasticbeanstalk/elasticbeanstalk-environment/tsconfig.json @@ -1,20 +1,23 @@ { "compilerOptions": { - "target":"ES2018", - "module": "commonjs", - "lib": ["es2016", "es2017.object", "es2017.string"], - "strict": true, - "noImplicitAny": true, - "strictNullChecks": true, - "noImplicitThis": true, - "alwaysStrict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": false, - "inlineSourceMap": true, - "inlineSources": true, - "experimentalDecorators": true, - "strictPropertyInitialization":false - } -} \ No newline at end of file + "target": "ES2020", + "module": "commonjs", + "lib": ["es2020"], + "declaration": true, + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "noImplicitThis": true, + "alwaysStrict": true, + "noUnusedLocals": false, + "noUnusedParameters": false, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": false, + "inlineSourceMap": true, + "inlineSources": true, + "experimentalDecorators": true, + "strictPropertyInitialization": false, + "typeRoots": ["./node_modules/@types"] + }, + "exclude": ["node_modules", "cdk.out"] +}