Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudflare example #260

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/cloudflare-example/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: pulumi-aws-cdk-fargate
runtime: nodejs
description: Fargate example for CDK
14 changes: 14 additions & 0 deletions examples/cloudflare-example/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:lts AS builder

WORKDIR /app

COPY package.json yarn.lock ./
COPY app ./

RUN yarn install --frozen-lockfile
RUN npx esbuild --bundle index.ts --target="node18" --platform="node" --outfile="index.js"

FROM node:18-alpine
COPY --from=builder /app/index.js /app/index.js
EXPOSE 80
CMD ["node", "/app/index.js"]
14 changes: 14 additions & 0 deletions examples/cloudflare-example/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express, { Request, Response } from 'express';
const app = express();

app.get('/', async (_req: Request, res: Response) => {
res.status(200).send('Hello, world!');
});

app.get('/health', (_req: Request, res: Response) => {
res.status(200).send(JSON.stringify({ message: 'OK' }));
});

app.listen(80, () => {
console.log('Listening on port 8080');
});
57 changes: 57 additions & 0 deletions examples/cloudflare-example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as path from 'path';
import * as pulumi from '@pulumi/pulumi';
import * as pulumicdk from '@pulumi/cdk';
import * as cloudflare from '@pulumi/cloudflare';
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import { Platform } from 'aws-cdk-lib/aws-ecr-assets';

const accountId = pulumi.secret(process.env['CLOUDFLARE_ACCOUNT_ID']!);
class CloudFlareStack extends pulumicdk.Stack {
public readonly nameservers: pulumi.Output<string[]>;
constructor(app: pulumicdk.App, id: string) {
super(app, id);

const zone = cloudflare.getZoneOutput({
accountId,
name: 'pulumi-cloudflare-demo.com',
});
this.nameservers = zone.nameServers;

const service = new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'service', {
listenerPort: 80,
taskImageOptions: {
image: ecs.ContainerImage.fromAsset(path.join(__dirname, './'), {
file: 'app/Dockerfile',
exclude: ['cdk.out', 'node_modules'],
// assetName is now required and is used in the name of the ecr repository that is created
assetName: 'cdk-cloudflare-example',
platform: Platform.LINUX_AMD64,
}),
},
});

new cloudflare.Record('alb', {
name: 'cdk-alb',
type: 'CNAME',
zoneId: zone.zoneId,
content: this.asOutput(service.loadBalancer.loadBalancerDnsName),
proxied: true,
});
}
}

class MyApp extends pulumicdk.App {
constructor() {
super('app', (scope: pulumicdk.App): pulumicdk.AppOutputs => {
const stack = new CloudFlareStack(scope, 'cloudflare');
return {
nameservers: stack.nameservers,
};
});
}
}

const app = new MyApp();
export const dns = 'http://cdk-alb.pulumi-cloudflare-demo.com';
export const nameservers = pulumi.unsecret(app.outputs['nameservers']);
20 changes: 20 additions & 0 deletions examples/cloudflare-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "pulumi-aws-cdk",
"devDependencies": {
"@types/node": "^10.0.0"
},
"dependencies": {
"@pulumi/aws": "^6.0.0",
"@pulumi/aws-native": "^1.9.0",
"@pulumi/cdk": "^0.5.0",
"@pulumi/cloudflare": "^5.43.1",
"@pulumi/docker-build": "^0.0.7",
"@pulumi/pulumi": "^3.0.0",
"@pulumi/tls": "^5.0.9",
"@types/express": "^5.0.0",
"aws-cdk-lib": "2.156.0",
"constructs": "10.3.0",
"esbuild": "^0.24.0",
"express": "^4.21.1"
}
}
18 changes: 18 additions & 0 deletions examples/cloudflare-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}