我意识到它很新,但我没有看到任何语言的示例如何为使用 AWS CDK 创建的 lambda 指定角色。
我试图这样做
const cdk = require('@aws-cdk/cdk');
const lambda = require('@aws-cdk/aws-lambda');
const iam = require('@aws-cdk/aws-iam');
const path = require('path');
class MyStack extends cdk.Stack {
constructor (parent, id, props) {
super(parent, id, props);
//
// Create a lambda...
const fn = new lambda.Function(this, 'MyFunction-cdktest', {
runtime: lambda.Runtime.NodeJS810,
handler: 'index.handler',
code: lambda.Code.directory( path.join( __dirname, 'lambda')),
role: iam.RoleName('lambda_basic_execution')
});
}
}
class MyApp extends cdk.App {
constructor (argv) {
super(argv);
new MyStack(this, 'hello-cdk');
}
}
console.log(new MyApp(process.argv).run());
Run Code Online (Sandbox Code Playgroud)
为了尝试为函数指定现有的 IAM …
我正在尝试使用 AWS CDK 构建应用程序,如果我要使用 AWS 控制台手动构建应用程序,我通常会在 API 网关中启用 CORS。
尽管我可以从 API Gateway 中导出 swagger 并找到了许多选项来为 OPTIONS 方法生成 Mock 端点,但我不知道如何使用 CDK 来做到这一点。目前我正在尝试:
const apigw = require('@aws-cdk/aws-apigateway');
Run Code Online (Sandbox Code Playgroud)
在哪里:
var api = new apigw.RestApi(this, 'testApi');
Run Code Online (Sandbox Code Playgroud)
并定义 OPTIONS 方法,如:
const testResource = api.root.addResource('testresource');
var mock = new apigw.MockIntegration({
type: "Mock",
methodResponses: [
{
statusCode: "200",
responseParameters : {
"Access-Control-Allow-Headers" : "string",
"Access-Control-Allow-Methods" : "string",
"Access-Control-Allow-Origin" : "string"
}
}
],
integrationResponses: [
{
statusCode: "200",
responseParameters: {
"Access-Control-Allow-Headers" : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
"Access-Control-Allow-Origin" : "'*'",
"Access-Control-Allow-Methods" …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AWS-CDK创建API网关,并使用Cognito用户池授权者保护REST端点。
我找不到任何例子。我认为应该看起来像这样,但是也许我不需要的方法不存在?
const cdk = require('@aws-cdk/cdk');
const lambda = require('@aws-cdk/aws-lambda');
const apigw = require('@aws-cdk/aws-apigateway');
const path = require('path');
//
// Define the stack:
class MyStack extends cdk.Stack {
constructor (parent, id, props) {
super(parent, id, props);
var tmethodHandler = new lambda.Function(this, 'test-lambda', {
runtime: lambda.Runtime.NodeJS810,
handler: 'index.handler',
code: lambda.Code.directory( path.join( __dirname, 'lambda')),
});
var api = new apigw.RestApi(this, 'test-api');
const tmethod = api.root.addResource('testmethod');
const tmethodIntegration = new apigw.LambdaIntegration(tmethodHandler);
tmethod.addMethod('GET', getSessionIntegration, {
authorizationType: apigw.AuthorizationType.Cognito,
authorizerId : 'crap!!!?'
});
}
}
class …
Run Code Online (Sandbox Code Playgroud)