Created
November 4, 2020 11:31
-
-
Save neilkuan/fd3efd80d07972f4da9648b90900a42b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import * as ec2 from '@aws-cdk/aws-ec2'; | |
| import * as ecs from '@aws-cdk/aws-ecs'; | |
| //import * as ecspat from '@aws-cdk/aws-ecs-patterns'; | |
| import * as iam from '@aws-cdk/aws-iam'; | |
| import { App, Construct, Stack, StackProps, CfnOutput } from '@aws-cdk/core'; | |
| export interface runnerMyStackProps extends StackProps{ | |
| gitlabRegistrationToken: string; | |
| tagLists?: string[]; | |
| } | |
| export class MyStack extends Stack { | |
| constructor(scope: Construct, id: string, props: runnerMyStackProps) { | |
| super(scope, id, props); | |
| // define resources here... | |
| const vpc = new ec2.Vpc(this, 'defaultVpc', { | |
| cidr: '10.2.0.0/16', | |
| natGateways: 1, | |
| subnetConfiguration: [{ | |
| cidrMask: 20, | |
| name: 'public', | |
| subnetType: ec2.SubnetType.PUBLIC, | |
| }, | |
| { | |
| cidrMask: 20, | |
| name: 'private', | |
| subnetType: ec2.SubnetType.PRIVATE, | |
| }], | |
| }); | |
| const cluster = new ecs.Cluster(this, 'runnerCluster', { | |
| vpc, | |
| clusterName: 'runnerCluster', | |
| }); | |
| const runnerFargateSG = new ec2.SecurityGroup(this, 'runnerFargateSG', { | |
| vpc, | |
| }); | |
| runnerFargateSG.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(22)); | |
| const runnerTaskDefinition = new ecs.FargateTaskDefinition(this, 'runnerTaskDefinition', { | |
| cpu: 512, | |
| memoryLimitMiB: 1024, | |
| }); | |
| const jobTaskDefinition = new ecs.TaskDefinition(this, 'runnerJobTaskDefinition', { | |
| cpu: '512', | |
| memoryMiB: '1024', | |
| compatibility: ecs.Compatibility.FARGATE, | |
| networkMode: ecs.NetworkMode.AWS_VPC, | |
| }); | |
| jobTaskDefinition.addContainer('ci-coordinator', { | |
| image: ecs.ContainerImage.fromRegistry('registry.gitlab.com/tmaczukin-test-projects/fargate-driver-debian:latest'), | |
| logging: new ecs.AwsLogDriver({ | |
| streamPrefix: 'runnerJobLoger', | |
| }), | |
| }).addPortMappings({ | |
| containerPort: 22, | |
| hostPort: 22, | |
| protocol: ecs.Protocol.TCP, | |
| }); | |
| runnerTaskDefinition.addContainer('runnerMaster', { | |
| image: ecs.ContainerImage.fromRegistry('registry.gitlab.com/danielcmiranda/docker-gitlab-runner-fargate-driver'), | |
| environment: { | |
| GITLAB_REGISTRATION_TOKEN: props.gitlabRegistrationToken, | |
| FARGATE_CLUSTER: cluster.clusterName, | |
| FARGATE_REGION: this.region, | |
| FARGATE_SECURITY_GROUP: runnerFargateSG.securityGroupName, | |
| FARGATE_SUBNET: vpc.privateSubnets[0].subnetId, | |
| FARGATE_TASK_DEFINITION: `${jobTaskDefinition}`, | |
| RUNNER_TAG_LIST: this.runnerTagsGenerator(props.tagLists ?? ['gitlab', 'fargate', 'runner']), | |
| }, | |
| logging: new ecs.AwsLogDriver({ | |
| streamPrefix: 'runnerFargateLoger', | |
| }), | |
| }); | |
| runnerTaskDefinition.addToTaskRolePolicy(new iam.PolicyStatement({ | |
| actions: ['ecs:*', 'iam:PassRole'], | |
| resources: ['*'], | |
| })); | |
| new ecs.FargateService(this, 'FargateService', { | |
| taskDefinition: runnerTaskDefinition, | |
| cluster, | |
| assignPublicIp: true, | |
| }); | |
| new CfnOutput(this, 'publicsubnetList', { | |
| value: this.getSubnetLists(vpc.publicSubnets), | |
| }); | |
| new CfnOutput(this, 'privatesubnetList', { | |
| value: this.getSubnetLists(vpc.privateSubnets), | |
| }); | |
| } | |
| private runnerTagsGenerator(tagLists: string[]) :string { | |
| const tagString = tagLists.join(','); | |
| return tagString; | |
| } | |
| private getSubnetLists(subnets :ec2.ISubnet[]) :string { | |
| let subnetList: string[] = []; | |
| subnets.forEach(element => { | |
| subnetList.push(element.subnetId); | |
| }); | |
| let subnetString:string = subnetList.join(','); | |
| return subnetString; | |
| } | |
| } | |
| // for development, use account/region from cdk cli | |
| const devEnv = { | |
| account: process.env.CDK_DEFAULT_ACCOUNT, | |
| region: process.env.CDK_DEFAULT_REGION, | |
| }; | |
| const app = new App(); | |
| new MyStack(app, 'my-stack-dev', { | |
| env: devEnv, | |
| gitlabRegistrationToken: `${process.env.GITLABTOKEN}`, | |
| tagLists: ['apple', 'banana'], | |
| }); | |
| // new MyStack(app, 'my-stack-prod', { env: prodEnv }); | |
| app.synth(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment