AWS Apigateway更改为Lamdarestapi,Suporting代理和自定义模型(表格/UrlenCoded)

发布于 2025-02-10 20:44:16 字数 2777 浏览 2 评论 0原文

因此,外部资源以形式/urlencoded登上了apigateway。 Apigateway从Dotnetlambda更改为Lamdarestapi。所有现有的常规JSON帖子都可以使用API​​工作,没有问题。

Slack在这里解决了此问题 http://wwwww.ryanray.me.me/serverless-serverless-slack-integrations < /a>,但仅通过Web控制台手动用于'application/x-www-form-urlenCoded; charset = utf-8'在AWS网关上,

试图在打字稿中使用现有的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2025-02-17 20:44:16

lamctaintegration具有和数量的IntergrationResspons,您可以在响应模式中添加代码。

const integrationResponse: IntegrationResponse = {
  statusCode: "200",
  contentHandling: ContentHandling.CONVERT_TO_TEXT
  allowTestInvoke: true,
  proxy: false,
  integrationResponses: [
  {
     statusCode: "200",
     responseTemplates: {
     "application/x-www-form-urlencoded": `
    #if ($context.httpMethod == "POST")
    ....rest of that code translation

`

LamdaIntegration has and array of intergrationResponses, which you can add code inside responseTemplates.

const integrationResponse: IntegrationResponse = {
  statusCode: "200",
  contentHandling: ContentHandling.CONVERT_TO_TEXT
  allowTestInvoke: true,
  proxy: false,
  integrationResponses: [
  {
     statusCode: "200",
     responseTemplates: {
     "application/x-www-form-urlencoded": `
    #if ($context.httpMethod == "POST")
    ....rest of that code translation

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文