Skip to content

Instantly share code, notes, and snippets.

@cosminilie
Created November 14, 2018 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminilie/5d9d1c7ac800ec995423357fba556862 to your computer and use it in GitHub Desktop.
Save cosminilie/5d9d1c7ac800ec995423357fba556862 to your computer and use it in GitHub Desktop.
import * as aws from "@pulumi/aws";
import { asset } from "@pulumi/pulumi";
let region = aws.config.requireRegion();
///////////////////
// Lambda Function
///////////////////
let policy = {
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com",
},
"Effect": "Allow",
"Sid": "",
},
],
};
let role = new aws.iam.Role("mylambda-role", {
assumeRolePolicy: JSON.stringify(policy),
});
let fullAccess = new aws.iam.RolePolicyAttachment("mylambda-access", {
role: role,
policyArn: aws.iam.AWSLambdaFullAccess,
});
let lambda = new aws.lambda.Function("mylambda", {
code: new asset.AssetArchive({
"index.js": new asset.StringAsset(
"exports.handler = (e, c, cb) => cb(null, {statusCode: 200, body: 'Hello, world!'});",
),
}),
role: role.arn,
handler: "index.handler",
runtime: aws.lambda.NodeJS6d10Runtime,
});
let restApi = new aws.apigateway.RestApi("myrestapi", {});
let resource = new aws.apigateway.Resource("myrestapi-resource", {
restApi: restApi,
pathPart: "bambam",
parentId: restApi.rootResourceId!,
});
let method = new aws.apigateway.Method("myrestapi-method", {
restApi: restApi,
resourceId: resource.id,
httpMethod: "ANY",
apiKeyRequired: true,
authorization: "NONE"
});
let integration = new aws.apigateway.Integration("myrestapi-integration", {
restApi: restApi,
resourceId: resource.id,
httpMethod: "ANY",
type: "AWS_PROXY",
integrationHttpMethod: "POST",
passthroughBehavior: "WHEN_NO_MATCH",
uri: lambda.arn.apply(arn =>
arn && "arn:aws:apigateway:" + region + ":lambda:path/2015-03-31/functions/" + arn + "/invocations"),
}, { dependsOn: [ method ] });
let deployment = new aws.apigateway.Deployment("myrestapi-deployment-prod", {
restApi: restApi,
description: "my deployment",
stageName: "prod",
}, { dependsOn: [ integration ] });
let permission = new aws.lambda.Permission("apigateway-permission", {
action: "lambda:InvokeFunction",
principal: "apigateway." + region + ".amazonaws.com",
sourceArn: deployment.executionArn,
function: lambda,
});
// const stage = new aws.apigateway.Stage("stage", {
// restApi: restApi,
// deployment: deployment,
// stageName: "prod",
// });
const apiKey = new aws.apigateway.ApiKey("api-key", {
enabled: true,
name: "test-key"
});
const usagePlan = new aws.apigateway.UsagePlan("usage-plan", {
apiStages: [{
apiId: restApi.id.apply(id => id),
stage: deployment.stageName
}],
name: "test"
});
const usagePlanKey = new aws.apigateway.UsagePlanKey("usage-plan-key", {
keyId: apiKey.id.apply(id => id),
keyType: "API_KEY",
usagePlanId: usagePlan.id.apply(id => id),
});
export const endpointURL = deployment.invokeUrl
export const key = apiKey.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment