Created
August 12, 2022 11:26
-
-
Save Pagebakers/48ccf3b0862286e5e07e1173e16b2758 to your computer and use it in GitHub Desktop.
wundergraph-stack.ts
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
# Image layer for building the application | |
FROM --platform=linux/amd64 node:16 as build | |
RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm | |
WORKDIR /usr/src/app | |
# rebuild image when package.json or lock has changed | |
COPY package.json pnpm-lock.yaml .npmrc ./ | |
ENV NODE_ENV="production" | |
# RUN pnpm fetch --prod | |
# add project artifacts to docker image | |
ADD . ./ | |
RUN pnpm install --prod | |
# install wunderctl in correct version | |
RUN curl -s -L https://github.com/wundergraph/wundergraph/releases/download/v0.94.5/wunderctl_0.94.5_Linux_x86_64.tar.gz | tar xzvf - && \ | |
chmod +x wunderctl && mv wunderctl /usr/local/bin/wunderctl | |
RUN cp /usr/local/bin/wunderctl /usr/src/app/node_modules/.pnpm/@[email protected]/node_modules/@wundergraph/wunderctl/download/wunderctl | |
RUN wunderctl version | |
# RUN pnpm wunderctl version | |
# generate your wundergraph application | |
RUN cd .wundergraph && wunderctl generate | |
# RUN wunderctl generate | |
# Image layer for production | |
FROM build as runner | |
WORKDIR /usr/src/app | |
# copy entire project and dependencies | |
COPY --from=build --chown=node:node /usr/src/app . | |
# copy wunderctl to start the server | |
COPY --from=build --chown=node:node /usr/local/bin/wunderctl /usr/local/bin/wunderctl | |
# RUN pnpm wunderctl version | |
# run as non-root user | |
USER node | |
WORKDIR /usr/src/app/.wundergraph | |
CMD wunderctl start --listen-addr 0.0.0.0:9991 --debug | |
EXPOSE 9991 |
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
const wunderNode = new WundergraphStack(scope, `${id}-wundergraph`, { | |
domainName: `wundergraph.${domainName}`, | |
}) |
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
import { join } from 'path' | |
import { aws_certificatemanager, Stack, StackProps } from 'aws-cdk-lib' | |
import * as ec2 from 'aws-cdk-lib/aws-ec2' | |
import { | |
DockerImageAsset, | |
NetworkMode, | |
Platform, | |
} from 'aws-cdk-lib/aws-ecr-assets' | |
import * as ecs from 'aws-cdk-lib/aws-ecs' | |
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns' | |
import { Construct } from 'constructs' | |
import { Certificate } from 'aws-cdk-lib/aws-certificatemanager' | |
export interface WundergraphStackProps extends StackProps { | |
/** | |
* The WunderNode domain, eg: wundergraph.yourapp.io | |
*/ | |
domainName: string | |
/** | |
* Certificate for the domain, can be a wildcard. | |
* Leave empty set it up automatically through route53 | |
*/ | |
certificate?: Certificate | |
/** | |
* Environment variables available in the Docker image. | |
*/ | |
environment?: { [key: string]: string } | |
/** | |
* { FOO: ecs.Secret.fromSecretsManager(Secret.fromSecretNameV2(scope, "MySecret", "Secret")) } | |
*/ | |
secrets?: { [key: string]: ecs.Secret } | |
/** | |
* Your VPC, will create a new one by default. | |
*/ | |
vpc?: ec2.Vpc | |
} | |
export class WundergraphStack extends Stack { | |
constructor(scope: Construct, id: string, props: WundergraphStackProps) { | |
super(scope, id, props) | |
const vpc = | |
props.vpc || | |
new ec2.Vpc(this, `${id}Vpc`, { | |
maxAzs: 3, // Default is all AZs in region | |
}) | |
const cluster = new ecs.Cluster(this, `${id}WunderGraphEcsCluster`, { | |
vpc: vpc, | |
}) | |
const asset = new DockerImageAsset(this, `${id}WunderGraphDockerImage`, { | |
directory: join(__dirname, '../..'), | |
platform: Platform.LINUX_AMD64, | |
networkMode: NetworkMode.DEFAULT, | |
}) | |
const certificate = | |
props.certificate || | |
new Certificate(this, `${id}WunderGraphCertificate`, { | |
domainName: props.domainName, | |
validation: aws_certificatemanager.CertificateValidation.fromDns(), | |
}) | |
// Create a load-balanced Fargate service and make it public | |
const service = new ecs_patterns.ApplicationLoadBalancedFargateService( | |
this, | |
`${id}WunderGraphFargateService`, | |
{ | |
cluster: cluster, // Required | |
cpu: 512, // Default is 256 | |
desiredCount: 1, // Default is 1 | |
certificate, | |
taskImageOptions: { | |
image: ecs.ContainerImage.fromDockerImageAsset(asset), | |
containerPort: 9991, | |
enableLogging: true, | |
environment: props.environment, | |
secrets: props.secrets, | |
}, | |
redirectHTTP: true, | |
memoryLimitMiB: 1024, // Default is 512 | |
publicLoadBalancer: true, // Default is false | |
} | |
) | |
service.targetGroup.configureHealthCheck({ | |
port: '443', | |
path: '/', | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment