Skip to content

Commit

Permalink
feat: add frontend container
Browse files Browse the repository at this point in the history
  • Loading branch information
briancaffey committed Nov 27, 2021
1 parent b9453f5 commit 43f92d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
35 changes: 31 additions & 4 deletions src/docker-ec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
},
});
Expand All @@ -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,
Expand All @@ -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}`,
});
}
}
2 changes: 1 addition & 1 deletion src/files/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ services:
ports: []

web:
image: nginx
image: ${FRONTEND_IMAGE_URI}
networks:
- traefik-public
deploy:
Expand Down
7 changes: 2 additions & 5 deletions src/integ/integ.docker-ec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

/**
Expand Down
2 changes: 1 addition & 1 deletion test/django-step-by-step

0 comments on commit 43f92d2

Please sign in to comment.