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

chore: adding tests for cloudfront and lambda #174

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/cloudfront-lambda-urls/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: cloudfront-lambda-urls
runtime: nodejs
description: cloudfront-lambda-urls CDK example
8 changes: 8 additions & 0 deletions examples/cloudfront-lambda-urls/index.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Handler, APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';

export const handler: Handler = async (event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> => {
return {
statusCode: 200,
body: 'Hello world!',
};
};
61 changes: 61 additions & 0 deletions examples/cloudfront-lambda-urls/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as pulumi from '@pulumi/pulumi';
import * as pulumicdk from '@pulumi/cdk';
import { Code, FunctionUrlAuthType, Runtime } from 'aws-cdk-lib/aws-lambda';
import {
Distribution,
experimental,
Function,
FunctionCode,
FunctionEventType,
FunctionRuntime,
KeyValueStore,
LambdaEdgeEventType,
} from 'aws-cdk-lib/aws-cloudfront';
import { FunctionUrlOrigin, S3Origin } from 'aws-cdk-lib/aws-cloudfront-origins';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Bucket } from 'aws-cdk-lib/aws-s3';

class CloudFrontAppStack extends pulumicdk.Stack {
public cloudFrontUrl: pulumi.Output<string>;
constructor(id: string) {
super(id);

const handler = new NodejsFunction(this, 'handler', {
runtime: Runtime.NODEJS_LATEST,
});

const cfFunction = new Function(this, 'CfFunction', {
code: FunctionCode.fromInline('export function handler(event) { return event.request }'),
runtime: FunctionRuntime.JS_2_0,
});

const alias = handler.addAlias('live');
const url = alias.addFunctionUrl({
authType: FunctionUrlAuthType.NONE,
});

const bucket = new Bucket(this, 'Bucket');

const distro = new Distribution(this, 'distro', {
defaultBehavior: {
origin: new FunctionUrlOrigin(url),
functionAssociations: [
{
function: cfFunction,
eventType: FunctionEventType.VIEWER_REQUEST,
},
],
},
});
distro.addBehavior('/images/*', new S3Origin(bucket));

new KeyValueStore(this, 'KVStore');

this.cloudFrontUrl = this.asOutput(distro.distributionDomainName);

this.synth();
}
}

const stack = new CloudFrontAppStack('cloudfront-app');
export const url = pulumi.interpolate`https://${stack.cloudFrontUrl}`;
17 changes: 17 additions & 0 deletions examples/cloudfront-lambda-urls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "cloudfront-lambda-urls",
"devDependencies": {
"@types/node": "^10.0.0"
},
"dependencies": {
"@pulumi/aws": "^4.6.0",
"@pulumi/aws-native": "^0.117.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the current major now? I guess there shouldn't be anything stopping us from doing that, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

"@pulumi/cdk": "^0.5.0",
"@pulumi/pulumi": "^3.0.0",
"@types/aws-lambda": "^8.10.145",
"aws-cdk-lib": "2.149.0",
"aws-lambda": "^1.0.7",
"constructs": "10.3.0",
"esbuild": "^0.24.0"
}
}
18 changes: 18 additions & 0 deletions examples/cloudfront-lambda-urls/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"
]
}
9 changes: 9 additions & 0 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func TestEC2Instance(t *testing.T) {
integration.ProgramTest(t, &test)
}

func TestCloudFront(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "cloudfront-lambda-urls"),
})

integration.ProgramTest(t, &test)
}

func TestAPIWebsocketLambdaDynamoDB(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Expand Down
Loading