From 43f92d2c9413ac602b4fdcdee9740d666c780e62 Mon Sep 17 00:00:00 2001 From: Brian Caffey Date: Sat, 27 Nov 2021 14:29:35 -0500 Subject: [PATCH] feat: add frontend container --- src/docker-ec2.ts | 35 +++++++++++++++++++++++++++++++---- src/files/stack.yml | 2 +- src/integ/integ.docker-ec2.ts | 7 ++----- test/django-step-by-step | 2 +- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/docker-ec2.ts b/src/docker-ec2.ts index d0bc3c8..e8d5e76 100644 --- a/src/docker-ec2.ts +++ b/src/docker-ec2.ts @@ -15,9 +15,14 @@ export interface DockerEc2Props { readonly imageDirectory: string; /** - * The command used to run the API web service. + * Frontend Image directory (nginx, quasar-app) */ - // readonly webCommand?: string[]; + readonly frontendImageDirectory: string; + + /** + * Frontend Image Dockerfile + */ + readonly frontendImageDockerfile: string; /* * Route 53 Zone Name, for example my-zone.com @@ -157,6 +162,14 @@ DJANGO_SETTINGS_MODULE=backend.settings.swarm_ec2 directory: props.imageDirectory, }); + const frontendImage = new ecrAssets.DockerImageAsset(this, 'FrontendImage', { + directory: props.frontendImageDirectory, + file: props.frontendImageDockerfile, + buildArgs: { + BACKEND_API_URL: `https://${props.domainName}`, + }, + }); + const contentStringInstallApplication = ` #!/bin/bash # download the stack.yml file @@ -165,6 +178,7 @@ docker swarm init docker network create --driver=overlay traefik-public export DOMAIN_NAME=${props.domainName} export IMAGE_URI=${backendImage.imageUri} +export FRONTEND_IMAGE_URI=${frontendImage.imageUri} # login to ecr aws ecr get-login-password --region ${stackRegion} | docker login --username AWS --password-stdin ${accountId}.dkr.ecr.${stackRegion}.amazonaws.com docker stack deploy --with-registry-auth -c stack.yml stack @@ -254,7 +268,7 @@ docker stack deploy --with-registry-auth -c stack.yml stack init, initOptions: { configSets: ['application'], - timeout: cdk.Duration.minutes(20), + timeout: cdk.Duration.minutes(10), includeUrl: true, }, }); @@ -264,8 +278,9 @@ docker stack deploy --with-registry-auth -c stack.yml stack iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEC2ContainerRegistryReadOnly'), ); - // allow the EC2 instance to access the ECR repository + // allow the EC2 instance to access the ECR repositories (TODO: figure out if this is needed) backendImage.repository.grantPull(instance.role); + frontendImage.repository.grantPull(instance.role); const hostedZone = route53.HostedZone.fromLookup(scope, 'hosted-zone', { domainName: props.zoneName, @@ -276,5 +291,17 @@ docker stack deploy --with-registry-auth -c stack.yml stack recordName: props.domainName, target: route53.RecordTarget.fromIpAddresses(instance.instancePublicIp), }); + + // Output values + + // Use this command to SSH to the machine + new cdk.CfnOutput(this, 'Ec2InstanceSshCommand', { + value: `ssh -i "~/.ssh/${props.keyName}.pem" ec2-user@${instance.instancePublicDnsName}`, + }); + + // site url + new cdk.CfnOutput(this, 'SiteUrl', { + value: `https://${props.domainName}`, + }); } } \ No newline at end of file diff --git a/src/files/stack.yml b/src/files/stack.yml index c8f0f93..dbcd480 100644 --- a/src/files/stack.yml +++ b/src/files/stack.yml @@ -91,7 +91,7 @@ services: ports: [] web: - image: nginx + image: ${FRONTEND_IMAGE_URI} networks: - traefik-public deploy: diff --git a/src/integ/integ.docker-ec2.ts b/src/integ/integ.docker-ec2.ts index 7e53daa..ef2febe 100644 --- a/src/integ/integ.docker-ec2.ts +++ b/src/integ/integ.docker-ec2.ts @@ -11,16 +11,13 @@ const stack = new cdk.Stack(app, 'DockerEc2Stack', { env }); const construct = new DockerEc2(stack, 'DockerEc2Sample', { imageDirectory: './test/django-step-by-step/backend', - // webCommand: [ - // './scripts/start_prod.sh', - // ], + frontendImageDirectory: './test/django-step-by-step', + frontendImageDockerfile: 'nginx/prod/Dockerfile', // s3BucketName: process.env.S3_BUCKET_NAME!, domainName: process.env.HOSTNAME!, keyName: 'jbc-aws', zoneName: process.env.ZONE_NAME!, // environmentVariables: { FOO_ENV_VAR: 'foo-env-var' }, - - // certificateArn: process.env.CERTIFICATE_ARN, }); /** diff --git a/test/django-step-by-step b/test/django-step-by-step index 6b6fdab..ec12027 160000 --- a/test/django-step-by-step +++ b/test/django-step-by-step @@ -1 +1 @@ -Subproject commit 6b6fdab01ce347d7dad1a877856402812ece9efd +Subproject commit ec120275319d57adc123c9cd37c9aaf399377e0e