sparse-intern-71089
07/09/2020, 4:18 PMlimited-solstice-34584
07/09/2020, 4:18 PMconst api = new awsx.apigateway.API("macs-dev", {
routes: [
{
path: "/",
method: "GET",
eventHandler: new aws.lambda.CallbackFunction("get-handler", {
memorySize: 128,
layers: [
"arn:aws:lambda:us-east-2:485017757204:layer:data-api-layer:1",
],
callback: async (event) => {
const AWS = require("aws-sdk");
exports.handler = async function (event, context, callback) {
const data = require("data-api-client")({
secretArn: secret.arn,
resourceArn: db.arn,
database: "macsgameboard", // default database
region: "us-east-2",
});
`return data.query(call getmiscdata();
);`
};
},
}),
apiKeyRequired: true,
},
],
apiKeySource: "HEADER",
});
const apikeys = awsx.apigateway.createAssociatedAPIKeys("my-api-keys", {
usagePlan: {
apiStages: [
{
apiId: api.restAPI.id,
stage: api.stage.stageName,
},
],
},
apiKeys: [{ name: "marketJS" }, { name: "macs-api-key" }],
});
limited-solstice-34584
07/09/2020, 4:19 PMlimited-solstice-34584
07/09/2020, 4:19 PMexports.handler = __f0;
var __exports = {};
var __secret = {};
var __secret_arn_proto = {};
__f1.prototype = __secret_arn_proto;
Object.defineProperty(__secret_arn_proto, "constructor", { configurable: true, writable: true, value: __f1 });
Object.defineProperty(__secret_arn_proto, "apply", { configurable: true, writable: true, value: __f2 });
Object.defineProperty(__secret_arn_proto, "get", { configurable: true, writable: true, value: __f3 });
var __secret_arn = Object.create(__secret_arn_proto);
__secret_arn.value = "arn:aws:secretsmanager:us-east-2:485017757204:secret:macsdb_password-d28f2fe-bTXrLB";
__secret.arn = __secret_arn;
var __db = {};
var __db_arn = Object.create(__secret_arn_proto);
__db_arn.value = "arn:aws:rds:us-east-2:485017757204:cluster:tf-20200709154701156900000001";
__db.arn = __db_arn;
function __f1(__0) {
return (function() {
with({ }) {
return function /*constructor*/(value) {
this.value = value;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f2(__0) {
return (function() {
with({ }) {
return function /*apply*/(func) {
throw new Error("'apply' is not allowed from inside a cloud-callback. Use 'get' to retrieve the value of this Output directly.");
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f3() {
return (function() {
with({ }) {
return function /*get*/() {
return this.value;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f0(__0) {
return (function() {
with({ exports: __exports, secret: __secret, db: __db }) {
return async (event) => {
const AWS = require("aws-sdk");
exports.handler = async function (event, context, callback) {
const data = require("data-api-client")({
secretArn: secret.arn,
resourceArn: db.arn,
database: "macsgameboard", // default database
region: "us-east-2",
});
`return data.query(call getmiscdata();
);`
};
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
limited-solstice-34584
07/09/2020, 4:20 PMlimited-solstice-34584
07/09/2020, 4:35 PMfaint-motherboard-95438
07/09/2020, 4:42 PMfaint-motherboard-95438
07/09/2020, 4:44 PMsecret.arn
and db.arn
?limited-solstice-34584
07/09/2020, 4:44 PMfaint-motherboard-95438
07/09/2020, 4:44 PMstring
, not pulumi.Output<string>
?limited-solstice-34584
07/09/2020, 4:50 PMlimited-solstice-34584
07/09/2020, 4:50 PMlimited-solstice-34584
07/09/2020, 4:50 PMlimited-solstice-34584
07/09/2020, 4:50 PMfaint-motherboard-95438
07/09/2020, 4:51 PMOutput
issuelimited-solstice-34584
07/09/2020, 4:52 PMlimited-solstice-34584
07/09/2020, 4:53 PMlimited-solstice-34584
07/09/2020, 4:53 PMcall getmiscdata();
);limited-solstice-34584
07/09/2020, 5:15 PMfaint-table-42725
07/09/2020, 5:23 PMexports.handler
— but note that the callback your providing is the handlerlimited-solstice-34584
07/09/2020, 5:30 PMfaint-table-42725
07/09/2020, 5:30 PMcallback: async (event) => {
const AWS = require("aws-sdk");
const data = require("data-api-client")({
secretArn: secret.arn.get(),
resourceArn: db.arn.get(),
database: "macsgameboard", // default database
region: "us-east-2",
});
return data.query(`call getmiscdata();`);
};
},
limited-solstice-34584
07/09/2020, 5:31 PMfaint-table-42725
07/09/2020, 5:32 PMaws.sdk
assuming you’ve imported as import * as aws from "@pulumi/aws"
limited-solstice-34584
07/09/2020, 5:38 PMlimited-solstice-34584
07/09/2020, 7:00 PMfaint-table-42725
07/09/2020, 8:11 PMlimited-solstice-34584
07/09/2020, 8:22 PMlimited-solstice-34584
07/09/2020, 8:22 PMfaint-table-42725
07/09/2020, 10:21 PM@pulumi/awsx
uses API Gateway v1, where for enabling CORS, you end up having to do what’s described in https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html — which includes setting up mock integration for OPTIONS
, etc. Unfortunately, there’s no built-in “enableCors” style functionality that abstracts that away in awsx.faint-table-42725
07/09/2020, 10:23 PMCorsConfiguration
faint-table-42725
07/09/2020, 10:24 PMlimited-solstice-34584
07/09/2020, 10:26 PMlimited-solstice-34584
07/09/2020, 10:26 PMfaint-table-42725
07/09/2020, 10:28 PMOPTIONS
to enable CORS. You can either do it the way shown there (though, slightly more costly/inefficient) since it’s using a lambda to return the appropriate response, or configure a mock integration response per the AWS docslimited-solstice-34584
07/09/2020, 11:16 PMlimited-solstice-34584
07/09/2020, 11:17 PMfaint-table-42725
07/09/2020, 11:33 PMlimited-solstice-34584
07/09/2020, 11:36 PMlimited-solstice-34584
07/09/2020, 11:36 PMfaint-table-42725
07/10/2020, 2:06 AMfaint-table-42725
07/10/2020, 2:06 AMOPTIONS
is simply what the mock integration would typicall dofaint-table-42725
07/10/2020, 2:08 AMfaint-table-42725
07/10/2020, 2:12 AMfaint-table-42725
07/10/2020, 2:13 AMlimited-solstice-34584
07/10/2020, 2:22 AMfaint-table-42725
07/10/2020, 2:22 AMfaint-table-42725
07/10/2020, 2:22 AMlimited-solstice-34584
07/10/2020, 2:27 AMfaint-table-42725
07/10/2020, 2:40 AMlimited-solstice-34584
07/10/2020, 2:41 AMfaint-table-42725
07/10/2020, 2:44 AMComponentResource
is simply an abstraction to represent a ‘thing’ that is made up of multiple underlying custom resourcesfaint-table-42725
07/10/2020, 2:45 AMmakeCorsPath(…)
which takes the appropriate args and generates all the relevant resourceslimited-solstice-34584
07/10/2020, 2:45 AMfaint-table-42725
07/10/2020, 2:47 AMfaint-table-42725
07/10/2020, 2:49 AMfunction enableCorsForPath(path: string) {
new Method(`${name}-options`), { ... });
new Integration(`${name}-integration`, { ... });
...
}
faint-table-42725
07/10/2020, 2:50 AMenableCorsForPath("/api/path")
in your Pulumi programlimited-solstice-34584
07/10/2020, 2:54 AMlimited-solstice-34584
07/10/2020, 2:54 AMlimited-solstice-34584
07/10/2020, 2:54 AMfaint-table-42725
07/10/2020, 3:16 AMlimited-solstice-34584
07/10/2020, 3:19 AMlimited-solstice-34584
07/10/2020, 3:19 AMfaint-table-42725
07/10/2020, 3:24 AMfaint-table-42725
07/10/2020, 3:26 AMGET
on some path /foo
) and so my Lambda must return the appropriate headers in its response. This is easy. I just have to include that in my response, e.g.
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "<https://www.example.com>",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
},
body: JSON.stringify(resp),
}
faint-table-42725
07/10/2020, 3:27 AMOPTIONS
request that a browser is going to send usfaint-table-42725
07/10/2020, 3:27 AMfaint-table-42725
07/10/2020, 3:27 AMOPTIONS
which simply returns the headers with an empty bodyfaint-table-42725
07/10/2020, 3:28 AMfaint-table-42725
07/10/2020, 3:28 AMOPTIONS
on that path instead, which involves having the IntegrationResponse
etc.faint-table-42725
07/10/2020, 3:30 AMComponentResource
or some other abstraction (say a function in your program itself) that will give you back a thing that encapsulates what we just talked about — either creating the simple Lambda callback that returns the headers w/ empty body or the set of various API Gateway resources (Resource, Integration, Method, IntegrationResponse) to give back the correct mock response.limited-solstice-34584
07/10/2020, 3:31 AMlimited-solstice-34584
07/10/2020, 3:31 AMfaint-table-42725
07/10/2020, 3:39 AMlimited-solstice-34584
07/10/2020, 3:40 AMlimited-solstice-34584
07/10/2020, 3:40 AMfunction enableCorsForPath(name) {
`const resource = new aws.apigateway.Resource("${name}-resource
, {`
parentId: api.rootResourceId,
pathPart: name,
restApi: api.id,
});
`const method = new aws.apigateway.Method("${name}-method"
, {`
authorization: "NONE",
httpMethod: "GET",
resourceId: resource.id,
restApi: api.id,
});
`const integration = new aws.apigateway.Integration(${name}-integration
, {`
httpMethod: method.httpMethod,
resourceId: resource.id,
restApi: api.id,
type: "MOCK",
});
const response200 = new aws.apigateway.MethodResponse("response_200", {
httpMethod: method.httpMethod,
resourceId: resource.id,
restApi: api.id,
statusCode: "200",
});
const integrationresponse = new aws.apigateway.IntegrationResponse(
``${name}-integrationresponse`,`
{
httpMethod: method.httpMethod,
resourceId: resource.id,
// Transforms the backend JSON response to XML
responseTemplates: {},
restApi: api.id,
statusCode: response200.statusCode,
headers: {
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET",
},
}
);
}
limited-solstice-34584
07/10/2020, 3:42 AMfaint-table-42725
07/10/2020, 3:55 AMapi
and parenting and some other things.faint-table-42725
07/10/2020, 3:55 AMfaint-table-42725
07/10/2020, 3:56 AMComponentResource
or parent
so that you can nest some resources of these w/in your resource graphlimited-solstice-34584
07/10/2020, 3:59 AM