GVKun编程网logo

在 oracle 云中实现端点的 AWS API 网关(oracle cloud vps)

1

对于在oracle云中实现端点的AWSAPI网关感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍oraclecloudvps,并为您提供关于.netC#APIGW在发送到AWSSQS时触发AWS

对于在 oracle 云中实现端点的 AWS API 网关感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍oracle cloud vps,并为您提供关于.net C# API GW 在发送到 AWS SQS 时触发 AWS Lambda 返回“连接被拒绝”、Amazon API Gateway:有没有办法像在 AWS 控制台中测试 REST API 一样测试 HTTP API?、AWS API Gateway 与 Cloudformation 脚本中的 AWS Event Bridge(Cloudwatch Events) 集成、AWS API Gateway 无法使用 JSON 数组负载调用 AWS Lambda 函数的有用信息。

本文目录一览:

在 oracle 云中实现端点的 AWS API 网关(oracle cloud vps)

在 oracle 云中实现端点的 AWS API 网关(oracle cloud vps)

如何解决在 oracle 云中实现端点的 AWS API 网关

我必须从 openapi 版本 3 文件(swagger)设置和配置 aws api 网关,端点在 oracle 云中的其他应用程序中实现。有没有人面临类似的架构以及如何实现它?要使用的 aws api 网关集成请求类型(vpc_link、http、aws 服务...)。 任何想法将不胜感激,提前致谢。 问候,

.net C# API GW 在发送到 AWS SQS 时触发 AWS Lambda 返回“连接被拒绝”

.net C# API GW 在发送到 AWS SQS 时触发 AWS Lambda 返回“连接被拒绝”

如何解决.net C# API GW 在发送到 AWS SQS 时触发 AWS Lambda 返回“连接被拒绝”

我的微不足道的 .net C# AWS Lambda 函数由 HTTP GET 触发到 API GW 运行,但在发送到 AWS SQS 时返回“连接被拒绝”

responseSendMsg = await sqsClient.SendMessageAsync(sendMessageRequest);

问题似乎是对 SQS 的调用。

这里是完整代码,90%由Visual Studio 2019生成,AWS Toolkit for Visual Studio,新AWS项目模板“AWS Serverless Application (.NET Core - C#)”,我只有:

  1. 将 lambda 函数处理程序方法签名更新为异步 通过任务,我遵循了 https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html#csharp-handler-async
  2. 中记录的模式
public async Task<APIGatewayProxyResponse> Get(APIGatewayProxyRequest request,ILambdaContext context)
  1. 添加到写入 SQS 的代码中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.SQS;
using TEST.SQS;
using Amazon.SQS.Model;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace TestAsyncFromLambda
{
    public class Functions
    {
        public Functions()
        {
        }

        public async Task<APIGatewayProxyResponse> Get(APIGatewayProxyRequest request,ILambdaContext context)
        {
            Console.WriteLine("Get Request\\n");

            // Call a local async method to prove that works,leaving SQS out of the situation for the moment
            await Method1();


            // Create the Amazon SQS client
            var clientConfig = new AmazonSQSConfig
            {
                ServiceURL = SQSConstants.AWS_SERVICE_URL,};
            var sqsClient = new AmazonSQSClient(clientConfig);

            // Create and initialize a SendMessageRequest instance
            SendMessageRequest sendMessageRequest = new SendMessageRequest();
            sendMessageRequest.QueueUrl = SQSConstants.MyQueueUrl;
            sendMessageRequest.MessageBody = "this is a test message";

            Console.WriteLine($"About to send message to queue: {sendMessageRequest.QueueUrl}");

            // Send the SQS message using "await"
            SendMessageResponse responseSendMsg = null;
            try
            {
                responseSendMsg =
                await sqsClient.SendMessageAsync(sendMessageRequest);
                //responseSendMsg =
                //sqsClient.SendMessageAsync(sendMessageRequest).GetAwaiter().GetResult(); ;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw e;
            }

            Console.WriteLine("SendMessage() complete");

            var response = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,Body = "Hello AWS Serverless",Headers = new Dictionary<string,string> { { "Content-Type","text/plain" } }
            };

            return response;
        }

        public static async Task Method1()
        {
            await Task.Run(() =>
            {
                int iterations = 3;
                for (int i = 1; i <= iterations; i++)
                {
                    Console.WriteLine(" Method 1,i=" + i + " of " + iterations);
                    // Do something
                    Task.Delay(100).Wait();
                }
            });
        }
    }
}

显然这是一个简化的例子,我计划在函数中做很多 SQS 处理,并期望使用 async 会有所帮助。

云监视日志显示该函数已被触发,直到发送到 SQS 调用,然后抛出异常:

System.Net.Http.HttpRequestException: Connection refused
2021-06-24T16:13:54.751+10:00   START RequestId: MyID-91a3-f1e61c3c3f7a Version: $LATEST
2021-06-24T16:13:55.240+10:00   Get Request
2021-06-24T16:13:55.261+10:00   Method 1,i=1 of 3
2021-06-24T16:13:55.380+10:00   Method 1,i=2 of 3
2021-06-24T16:13:55.481+10:00   Method 1,i=3 of 3
2021-06-24T16:13:56.160+10:00   About to send message to queue: https://sqs.ap-southeast-2.amazonaws.com/MyID/MyQueue.fifo
2021-06-24T16:14:32.503+10:00   System.Net.Http.HttpRequestException: Connection refused
2021-06-24T16:14:32.503+10:00   ---> System.Net.sockets.socketException (111): Connection refused
2021-06-24T16:14:32.503+10:00   at System.Net.Http.ConnectHelper.ConnectAsync(String host,Int32 port,CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   --- End of inner exception stack trace ---
2021-06-24T16:14:32.503+10:00   at System.Net.Http.ConnectHelper.ConnectAsync(String host,CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   at System.Net.Http.httpconnectionPool.ConnectAsync(HttpRequestMessage request,Boolean allowHttp2,CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   at System.Net.Http.httpconnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request,CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   at System.Net.Http.httpconnectionPool.GethttpconnectionAsync(HttpRequestMessage request,CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   at System.Net.Http.httpconnectionPool.SendWithRetryAsync(HttpRequestMessage request,Boolean doRequestAuth,CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask,HttpRequestMessage request,CancellationTokenSource cts,Boolean disposeCts)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.SQS.Internal.ValidationResponseHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.EndpointdiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.EndpointdiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
2021-06-24T16:14:32.503+10:00   at TestAsyncFromLambda.Functions.Get(APIGatewayProxyRequest request,ILambdaContext context) in 

我相信我使用的一切都是最新版本:

enter image description here

我还尝试了 C# async await 语法的其他一些排列,但结果相同。

responseSendMsg = sqsClient.SendMessageAsync(sendMessageRequest).GetAwaiter().GetResult(); ;

API GW、Lambda 或 SQS 不涉及 VPC,一切都在同一个 AWS 账户中,只是一个基本的简单设置。

我还做了一个测试,我将 Lambda 发送到的 SQS 中的队列 URL 更改为不存在的无效队列名称,并且我得到了相同的行为,因此这意味着 SQS 发送请求可能永远不会得到到 AWS。

对于适用于 SQS 的 AWS .net SDK,在使用 SQS 的同步和异步方法之间没有选择,只有异步:https://docs.aws.amazon.com/sdk-for-net/latest/developer-guide/SendMessage.html。

Lambda 函数附加到具有这些权限的角色,我认为应该足够了:

enter image description here

我什至添加了“AdministratorAccess”角色,所以我假设只要 Lambda 和 SQS 在同一个 AWS 账户上,Lambda 就可以访问发送到 SQS。

文件 aws-lambda-tools-defaults.json 有:

{
    "information" : [
        "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.","To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.","dotnet lambda help","All the command line options for the Lambda command can be specified in this file."
    ],"profile"     : "myProfile","region"      : "ap-southeast-2","configuration" : "Release","framework"     : "netcoreapp3.1","s3-prefix"     : "TestAsyncFromLambda/","template"      : "serverless.template","template-parameters" : "","s3-bucket"           : "awsserverless2stack-bucket-myID","stack-name"          : "TestAsyncFromLambda"
}

文件 serverless.template 是:

{
  "AWstemplateFormatVersion": "2010-09-09","Transform": "AWS::Serverless-2016-10-31","Description": "An AWS Serverless Application by matt.","Resources": {
    "Get": {
      "Type": "AWS::Serverless::Function","Properties": {
        "Handler": "TestAsyncFromLambda::TestAsyncFromLambda.Functions::Get","Runtime": "dotnetcore3.1","CodeUri": "","MemorySize": 256,"Timeout": 187,"Role": null,"Policies": [
          "AWSLambdaBasicExecutionRole","AmazonSQSFullAccess","AdministratorAccess"
        ],"Events": {
          "RootGet": {
            "Type": "Api","Properties": {
              "Path": "/","Method": "GET"
            }
          }
        }
      }
    }
  },"Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for Prod environment","Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
      }
    }
  }
}

我已尝试通过 AWS 控制台 Web UI 和 Cloud Formation 脚本手动创建队列,示例取自 here:

{
  "AWstemplateFormatVersion": "2010-09-09","Resources": {
    "MyQueue": {
      "Properties": {
        "QueueName": "MyQueue.fifo","FifoQueue": true,"ContentBasedDeduplication": true
      },"Type": "AWS::SQS::Queue"
    }
  },"Outputs": {
    "QueueName": {
      "Description": "The name of the queue","Value": {
        "Fn::GetAtt": [
          "MyQueue","QueueName"
        ]
      }
    },"QueueURL": {
      "Description": "The URL of the queue","Value": {
        "Ref": "MyQueue"
      }
    },"QueueARN": {
      "Description": "The ARN of the queue","Arn"
        ]
      }
    }
  }
}

我将 lambda 超时增加到一个很长的自定义值:

  1. 区分该持续时间的超时与其他超时
  2. 给 SQS 发送更多时间以重试和/或超时

我通过更改 serverless.template 来增加超时时间:

"Timeout": 187,

在增加超时之前,当它是默认的30秒时,我在云监视日志中没有异常,就像Lambda超时一样。

解决方法

正如我在评论中提到的,用于 AWS 服务 URL 和队列 URL 的 URL 值似乎不正确。这就是发送消息时连接失败的原因。

因此您需要确保 SQSConstants.AWS_SERVICE_URLSQSConstants.MyQueueUrl 具有正确的值。

Amazon API Gateway:有没有办法像在 AWS 控制台中测试 REST API 一样测试 HTTP API?

Amazon API Gateway:有没有办法像在 AWS 控制台中测试 REST API 一样测试 HTTP API?

如何解决Amazon API Gateway:有没有办法像在 AWS 控制台中测试 REST API 一样测试 HTTP API?

我正在使用 AWS Lambda 和 Amazon API Gateway 构建 HTTP API。
在AWS控制台中,我可以通过Method Execution来测试REST APIs,但是在HTTP APIs配置页面中找不到类似的方式。

我怎样才能完成我想要的?
有没有好的方法可以做到这一点,或者出于某种原因没有这样的方法?

谢谢!

解决方法

如果不将路由+方法部署到阶段,然后使用 HTTP 客户端调用阶段的端点 URL,目前无法测试路由+方法的集成。

当您创建新的 HTTP API 时,您将获得一个默认阶段 $default,该阶段启用了自动部署,因此每当您对 API 进行更改时,这些更改将通过 API 可见端点可以立即使用 HTTP 客户端进行测试。

AWS API Gateway 与 Cloudformation 脚本中的 AWS Event Bridge(Cloudwatch Events) 集成

AWS API Gateway 与 Cloudformation 脚本中的 AWS Event Bridge(Cloudwatch Events) 集成

如何解决AWS API Gateway 与 Cloudformation 脚本中的 AWS Event Bridge(Cloudwatch Events) 集成

原始要求

在 AWS Api Gateway 上创建一个路由/路径,将 API Gateway 直接连接到 AWS Event Bridge (Cloudwatch Events) 并将事件放置/推送到它的事件总线上。

能够创建它并在从 AWS 控制台完成后执行得很好。

实际问题

为该 API 网关编写 AWS Cloudformation 脚本时,如下所示:

  1. EventsPostMethod:
  2. Type: AWS::ApiGateway::Method
  3. Properties:
  4. ResourceId:
  5. Ref: EventsResource
  6. RestApiId:
  7. Ref: RestAPI
  8. HttpMethod: POST
  9. AuthorizationType: NONE
  10. Integration:
  11. Type: AWS
  12. IntegrationHttpMethod: POST
  13. Uri:
  14. Fn::Sub: arn:aws:apigateway:${AWS::Region}:cloudwatchEvents:action/PutEvents
  15. RequestParameters:
  16. integration.request.header.X-Amz-Target: "''AWSEvents.PutEvents''"
  17. RequestTemplate:
  18. some-script-here...

注意 Uri 值:

  1. "arn:aws:apigateway:${AWS::Region}:cloudwatchEvents:action/PutEvents"
  2. arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}

根据 AWS Docs,uri 的值应该如下:

对于 AWS 或 AWS_PROXY 集成,URI 的格式为 arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}。此处,{Region} 是 API 网关区域(例如,us-east-1); {service} 是集成 AWS 服务的名称(例如,s3); {subdomain} 是某些 AWS 服务支持的指定子域,用于快速查找主机名。 action 可用于基于 AWS 服务操作的 API,使用 Action={name}&{p1}={v1}&p2={v2}... 查询字符串。随后的 {service_api} 指的是受支持的操作 {name} 以及任何必需的输入参数。或者,路径可用于基于 AWS 服务路径的 API。随后的 service_api 指的是 AWS 服务资源的路径,包括集成 AWS 服务的区域(如果适用)。例如,为了与 Getobject 的 S3 API 集成,uri 可以是 arn:aws:apigateway:us-west-2:s3:action/Getobject&Bucket={bucket}&Key={key} 或 arn:aws:apigateway: us-west-2:s3:path/{bucket}/{key}

您一定已经注意到,我将上述 uri 中的 service 替换为 cloudwatchEvents

现在,在发布 API 网关期间 AWS Cloudformation 控制台给出错误:

不支持 cloudwatchEvents 类型的 AWS 服务(服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:07bae22c-d198-4595-8de9-6ea23763eff5;代理:null)

现在我尝试用

替换服务
  • 云观察
  • eventBridge
  • cloudwatchEvent
  • 事件总线

这才是真正的问题。我应该在 uri 中放置什么才能接受?

enter image description here

解决方法

根据评论,

事件的 URI 应如下所示:

arn:aws:apigateway:${AWS::Region}:events:action/PutEvents

AWS API Gateway 无法使用 JSON 数组负载调用 AWS Lambda 函数

AWS API Gateway 无法使用 JSON 数组负载调用 AWS Lambda 函数

如何解决AWS API Gateway 无法使用 JSON 数组负载调用 AWS Lambda 函数

我在 API Gateway 中定义了一个资源/方法来对 Lambda 函数进行后期调用,这是一个暴露了几个端点的微服务。

方法:post

有效载荷:

  1. [{"gts_user_id":2,"gts_role_id":2,"gts_user_role_is_primary":1}]

enter image description here

此调用未到达 Lambda 方法。我看不到任何日志。 可能是什么原因?

解决方法

但是在分析和探索了几个选项后,我发现存在策略错误“最终策略大小(20577)大于限制(20480)”。我删除了触发器并重新创建,然后它开始工作。

我们今天的关于在 oracle 云中实现端点的 AWS API 网关oracle cloud vps的分享已经告一段落,感谢您的关注,如果您想了解更多关于.net C# API GW 在发送到 AWS SQS 时触发 AWS Lambda 返回“连接被拒绝”、Amazon API Gateway:有没有办法像在 AWS 控制台中测试 REST API 一样测试 HTTP API?、AWS API Gateway 与 Cloudformation 脚本中的 AWS Event Bridge(Cloudwatch Events) 集成、AWS API Gateway 无法使用 JSON 数组负载调用 AWS Lambda 函数的相关信息,请在本站查询。

本文标签: