-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Prudhvi Godithi <[email protected]>
- Loading branch information
1 parent
d8b52e6
commit 2888b62
Showing
4 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import {Aspects, Duration, Stack, Tag, Tags} from 'aws-cdk-lib'; | ||
import { | ||
AutoScalingGroup, | ||
BlockDeviceVolume, | ||
CfnLaunchConfiguration, | ||
HealthCheck, | ||
UpdatePolicy | ||
} from 'aws-cdk-lib/aws-autoscaling'; | ||
import { | ||
InstanceClass, | ||
InstanceSize, | ||
InstanceType, | ||
MachineImage, | ||
SubnetType, | ||
Vpc | ||
} from 'aws-cdk-lib/aws-ec2'; | ||
import { | ||
ArnPrincipal, | ||
CompositePrincipal, | ||
Effect, | ||
ManagedPolicy, | ||
PolicyStatement, | ||
Role, | ||
ServicePrincipal | ||
} from 'aws-cdk-lib/aws-iam'; | ||
import { Construct } from 'constructs'; | ||
import {OpenSearchMetricsSecretsStack} from "./secrets"; | ||
import {Secret} from "aws-cdk-lib/aws-secretsmanager"; | ||
|
||
|
||
export interface GitHubAppProps { | ||
readonly vpc: Vpc; | ||
readonly region: string; | ||
readonly account: string; | ||
readonly ami?: string | ||
readonly secret: Secret; | ||
} | ||
|
||
|
||
export class GitHubAutomationApp extends Stack { | ||
|
||
readonly asg: AutoScalingGroup; | ||
readonly githubAppRole: Role; | ||
|
||
constructor(scope: Construct, id: string, props: GitHubAppProps) { | ||
super(scope, id); | ||
|
||
const instanceRole = this.createInstanceRole(props.secret.secretArn, props.account); | ||
this.githubAppRole = instanceRole; | ||
|
||
this.asg = new AutoScalingGroup(this, 'OpenSearchMetrics-GitHubAppAsg', { | ||
instanceType: InstanceType.of(InstanceClass.M5, InstanceSize.LARGE), | ||
blockDevices: [{ deviceName: '/dev/xvda', volume: BlockDeviceVolume.ebs(10) }], | ||
healthCheck: HealthCheck.ec2({ grace: Duration.seconds(90) }), | ||
machineImage: props && props.ami ? | ||
MachineImage.fromSsmParameter(props.ami) : | ||
MachineImage.latestAmazonLinux2(), | ||
associatePublicIpAddress: false, | ||
allowAllOutbound: true, | ||
desiredCapacity: 1, | ||
minCapacity: 1, | ||
vpc: props.vpc, | ||
vpcSubnets: { | ||
subnetType: SubnetType.PRIVATE_WITH_EGRESS | ||
}, | ||
role: instanceRole, | ||
updatePolicy: UpdatePolicy.replacingUpdate() | ||
}); | ||
Tags.of(this.asg).add("Name", "OpenSearchMetrics-GitHubAutomationApp") | ||
|
||
|
||
const launchConfiguration = this.asg.node.findChild('LaunchConfig') as CfnLaunchConfiguration; | ||
launchConfiguration.metadataOptions = { | ||
httpPutResponseHopLimit: 2, | ||
httpEndpoint: "enabled", | ||
httpTokens: "required" | ||
}; | ||
|
||
const instanceName = 'OpenSearchMetrics-GitHubAppHost'; | ||
Aspects.of(this.asg).add(new Tag('name', instanceName, { | ||
applyToLaunchedInstances: true, | ||
includeResourceTypes: ['AWS::AutoScaling::AutoScalingGroup'] | ||
}),); | ||
this.asg.addUserData(...this.getUserData(props.secret.secretName)); | ||
} | ||
|
||
private createInstanceRole(secretArn: string, account: string): Role { | ||
const role = new Role(this, "OpenSearchMetrics-GitHubAppRole", { | ||
assumedBy: new CompositePrincipal( | ||
new ServicePrincipal('ec2.amazonaws.com'), | ||
new ArnPrincipal(`arn:aws:iam::${account}:role/OpenSearchMetrics-GitHubAppRole`) | ||
), | ||
roleName: "OpenSearchMetrics-GitHubAppRole", | ||
}); | ||
role.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore')); | ||
role.addToPolicy( | ||
new PolicyStatement({ | ||
effect: Effect.ALLOW, | ||
actions: ["secretsmanager:GetSecretValue"], | ||
resources: [secretArn], | ||
}), | ||
); | ||
role.addToPolicy( | ||
new PolicyStatement({ | ||
effect: Effect.ALLOW, | ||
actions: ["sts:AssumeRole"], | ||
resources: [role.roleArn], | ||
}), | ||
); | ||
return role; | ||
} | ||
|
||
|
||
private getUserData(secretName: string): string[] { | ||
return [ | ||
'sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm', | ||
'sudo dnf update -y', | ||
'sudo yum install git docker -y', | ||
'sudo systemctl enable docker', | ||
'sudo systemctl start docker', | ||
'sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/sbin/docker-compose', | ||
'sudo chmod a+x /usr/local/sbin/docker-compose', | ||
'git clone https://github.com/opensearch-project/automation-app.git', | ||
`aws secretsmanager get-secret-value --secret-id ${secretName} --query SecretString --output text >> automation-app/.env`, | ||
'cd automation-app/docker', | ||
'PORT=8080 RESOURCE_CONFIG=configs/resources/opensearch-project-resource.yml OPERATION_CONFIG=configs/operations/github-merged-pulls-monitor.yml docker-compose -p automation-app-1 up -d', | ||
'PORT=8081 RESOURCE_CONFIG=configs/resources/opensearch-project-resource.yml OPERATION_CONFIG=configs/operations/github-workflow-runs-monitor.yml docker-compose -p automation-app-2 up -d' | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { App } from "aws-cdk-lib"; | ||
import {Match, Template} from "aws-cdk-lib/assertions"; | ||
import Project from "../lib/enums/project"; | ||
import { VpcStack } from "../lib/stacks/vpc"; | ||
import {GitHubAutomationApp} from "../lib/stacks/gitHubAutomationApp"; | ||
import {OpenSearchMetricsSecretsStack} from "../lib/stacks/secrets"; | ||
|
||
|
||
test('OpenSearch GitHub App Stack test ', () => { | ||
const app = new App(); | ||
const vpcStack = new VpcStack(app, "Test-OpenSearchHealth-VPC", {}); | ||
const openSearchMetricsGitHubAppSecretStack = new OpenSearchMetricsSecretsStack(app, "Test-OpenSearchMetrics-GitHubAppSecret", { | ||
secretName: 'test-github-app-creds' | ||
}); | ||
const gitHubApp = new GitHubAutomationApp(app, "Test-OpenSearchMetrics-GitHubAutomationApp", { | ||
vpc: vpcStack.vpc, | ||
region: Project.REGION, | ||
account: Project.AWS_ACCOUNT, | ||
ami: Project.EC2_AMI_SSM.toString(), | ||
secret: openSearchMetricsGitHubAppSecretStack.secret | ||
}); | ||
|
||
const template = Template.fromStack(gitHubApp); | ||
template.resourceCountIs('AWS::IAM::Role', 1); | ||
template.hasResourceProperties('AWS::IAM::Role', { | ||
AssumeRolePolicyDocument: { | ||
Statement: Match.arrayWith([ | ||
{ | ||
Action: "sts:AssumeRole", | ||
Effect: "Allow", | ||
Principal: { | ||
Service: "ec2.amazonaws.com" | ||
} | ||
} | ||
]) | ||
} | ||
}); | ||
template.hasResourceProperties('AWS::EC2::SecurityGroup', { | ||
GroupDescription: Match.stringLikeRegexp('Test-OpenSearchMetrics-GitHubAutomationApp/OpenSearchMetrics-GitHubAppAsg/InstanceSecurityGroup') | ||
}); | ||
template.resourceCountIs('AWS::IAM::Policy', 1); | ||
template.hasResourceProperties('AWS::IAM::Policy', { | ||
PolicyDocument: { | ||
Statement: Match.arrayWith([ | ||
Match.objectLike({ | ||
Action: "secretsmanager:GetSecretValue", | ||
Effect: "Allow", | ||
Resource: { | ||
"Fn::ImportValue": Match.stringLikeRegexp('Test-OpenSearchMetrics-GitHubAppSecret:ExportsOutputRefMetricsCreds.*') | ||
} | ||
}) | ||
]) | ||
} | ||
}); | ||
template.hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', { | ||
DesiredCapacity: "1", | ||
MaxSize: "1", | ||
MinSize: "1", | ||
}) | ||
}); |