AWS Apigateway更改为Lamdarestapi,Suporting代理和自定义模型(表格/UrlenCoded)
因此,外部资源以形式/urlencoded登上了apigateway。 Apigateway从Dotnetlambda更改为Lamdarestapi。所有现有的常规JSON帖子都可以使用API工作,没有问题。
试图在打字稿中使用现有的IAC来完成此操作。
这是IM试图工作的代码。我仍然需要代理:false对于下面的“任何其他端点”,而没有任何IntegratioInparms/lamDaintegration都可以使所有其他本地API调用可用。
关键是要在处理代理时使用apgigateway:true和一个自定义路径和模型,该路径和模型需要代理:false抱怨。
export class DotNetRestApiCustom extends Construct {
lambda: DotnetLambda;
api: LambdaRestApi;
constructor(scope: Construct, id: string, props: DotNetHttpLambdaApiProps) {
super(scope, id);
this.lambda = new DotnetLambda(
this,
`${props.name}ApiLambda`,
`${props.name}Api`,
props.lambdaPath,
props.functionHandler,
props.functionProps,
props.extraHashCodePaths
);
let domainName = props.fullDomainName ?? `${Utils.stage.toLowerCase()}.${props.domainTag}.internal.${props.dnsZone.zoneName}`;
if (!props.fullDomainName && props.internalDomain === false) domainName = `${Utils.stage.toLowerCase()}.${props.domainTag}.${props.dnsZone.zoneName}`;
this.api = new LambdaRestApi(this, `${props.name}Api4`, {
proxy: false,
// restApiName: `${Utils.stage}-${props.name}Api`,
handler: this.lambda,
domainName: {
domainName: domainName,
certificate: new Certificate(this, "Certificate", {
domainName: domainName,
validation: CertificateValidation.fromDns(props.dnsZone),
}),
endpointType: EndpointType.REGIONAL,
securityPolicy: SecurityPolicy.TLS_1_2,
},
});
const msgResources = this.api.root.addResource("person");
const msgResource = msgResources.addResource("{personId}");
const methodResponse: MethodResponse = {
statusCode: "200",
responseModels: {"application/json": Model.EMPTY_MODEL}
}
const integrationResponse: IntegrationResponse = {
statusCode: "200",
contentHandling: ContentHandling.CONVERT_TO_TEXT
}
const requestTemplate = {
//"field1" : "$input.params('field1')",
//or
"body" : "$input.json('$')",
}
const integrationParams = new LambdaIntegration(this.lambda, {
allowTestInvoke: true,
proxy: false,
integrationResponses: [integrationResponse],
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES,
requestTemplates: { "application/json": JSON.stringify(requestTemplate) },
});
this.api.root.addMethod("POST", integrationParams, {
methodResponses: [methodResponse],
});
new ARecord(this, "ApiDnsRecord", {
zone: props.dnsZone,
recordName: domainName,
target: RecordTarget.fromAlias(new ApiGateway(this.api)),
});
So an outside resource hits the ApiGateway with form/urlencoded. The ApiGateway was changed from DotnetLambda to LamdaRestApi. All existing regular json posts to the api work and have no issue.
Slack had solved this issue here http://www.ryanray.me/serverless-slack-integrations, but only manually via the web console for 'application/x-www-form-urlencoded; charset=utf-8' on aws gateway
Trying to use the existing IaC in typescript, to accomplish this.
Here is the code in which im trying to get to work. I still need proxy: false for 'ANY' other endpoint as the same code below without any integratioinParms/LamdaIntegration keeps all other local api calls working.
The key is to have the ApiGateway to handle as it was working with proxy:true and a custom path and model which requires proxy: false which is complaining about.
export class DotNetRestApiCustom extends Construct {
lambda: DotnetLambda;
api: LambdaRestApi;
constructor(scope: Construct, id: string, props: DotNetHttpLambdaApiProps) {
super(scope, id);
this.lambda = new DotnetLambda(
this,
`${props.name}ApiLambda`,
`${props.name}Api`,
props.lambdaPath,
props.functionHandler,
props.functionProps,
props.extraHashCodePaths
);
let domainName = props.fullDomainName ?? `${Utils.stage.toLowerCase()}.${props.domainTag}.internal.${props.dnsZone.zoneName}`;
if (!props.fullDomainName && props.internalDomain === false) domainName = `${Utils.stage.toLowerCase()}.${props.domainTag}.${props.dnsZone.zoneName}`;
this.api = new LambdaRestApi(this, `${props.name}Api4`, {
proxy: false,
// restApiName: `${Utils.stage}-${props.name}Api`,
handler: this.lambda,
domainName: {
domainName: domainName,
certificate: new Certificate(this, "Certificate", {
domainName: domainName,
validation: CertificateValidation.fromDns(props.dnsZone),
}),
endpointType: EndpointType.REGIONAL,
securityPolicy: SecurityPolicy.TLS_1_2,
},
});
const msgResources = this.api.root.addResource("person");
const msgResource = msgResources.addResource("{personId}");
const methodResponse: MethodResponse = {
statusCode: "200",
responseModels: {"application/json": Model.EMPTY_MODEL}
}
const integrationResponse: IntegrationResponse = {
statusCode: "200",
contentHandling: ContentHandling.CONVERT_TO_TEXT
}
const requestTemplate = {
//"field1" : "$input.params('field1')",
//or
"body" : "$input.json('
)",
}
const integrationParams = new LambdaIntegration(this.lambda, {
allowTestInvoke: true,
proxy: false,
integrationResponses: [integrationResponse],
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES,
requestTemplates: { "application/json": JSON.stringify(requestTemplate) },
});
this.api.root.addMethod("POST", integrationParams, {
methodResponses: [methodResponse],
});
new ARecord(this, "ApiDnsRecord", {
zone: props.dnsZone,
recordName: domainName,
target: RecordTarget.fromAlias(new ApiGateway(this.api)),
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
lamctaintegration具有和数量的IntergrationResspons,您可以在响应模式中添加代码。
`
LamdaIntegration has and array of intergrationResponses, which you can add code inside responseTemplates.
`