-
Notifications
You must be signed in to change notification settings - Fork 659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API GW Management - 403 #889
Comments
Hi @Dasio , |
I also cannot get messaging to clients to work with the Go SDK, v1 or v2. I mostly get 403s but when it doesn't error, I can still see in CloudWatch that the API has not been engaged. I was hopeful that this issue from the JS SDK, which seems to apply to Go as well, would lead to a solution, but the data didn't land anywhere; there was no error, but no json showing up in my client either. Using the package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi"
"github.com/aws/smithy-go"
)
func handler(ctx context.Context, req events.DynamoDBEvent) (events.APIGatewayProxyResponse, error) {
str := "https://" + apiid + ".execute-api." + rec.AWSRegion + ".amazonaws.com"
customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
if service == apigatewaymanagementapi.ServiceID && region == rec.AWSRegion {
return aws.Endpoint{
PartitionID: "aws",
URL: str,
SigningRegion: rec.AWSRegion,
}, nil
}
return aws.Endpoint{}, fmt.Errorf("unknown endpoint requested")
})
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(rec.AWSRegion),
config.WithEndpointResolver(customResolver),
)
if err != nil {
fmt.Println("cfg err")
}
svc := apigatewaymanagementapi.NewFromConfig(cfg)
b, err := json.Marshal("{a: 19894, b: 74156}")
if err != nil {
fmt.Println("error marshalling", err)
}
conn := apigatewaymanagementapi.PostToConnectionInput{
ConnectionId: aws.String(item["sk"].String()),
Data: b
}
o, e := svc.PostToConnection(ctx, &conn)
if e != nil {
// To get any API error
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
fmt.Printf("db error, Code: %v, Message: %v", apiErr.ErrorCode(), apiErr.ErrorMessage())
}
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{"Content-Type": "application/json"},
MultiValueHeaders: map[string][]string{},
Body: "",
IsBase64Encoded: false,
}, nil
}
func main() {
lambda.Start(handler)
} This seems like it should work. Here is a similar Python implementation from SO: import time
import json
import boto3
def lambda_handler(event, context):
connection_id = event["requestContext"]["connectionId"]
domain_name = event["requestContext"]["domainName"]
stage = event["requestContext"]["stage"]
message = f'{domain_name}: {connection_id}'.encode('utf-8')
api_client = boto3.client('apigatewaymanagementapi', endpoint_url = f"https://{domain_name}/{stage}")
for _ in range(5):
api_client.post_to_connection(Data=message,
ConnectionId=connection_id)
time.sleep(5)
response = {'statusCode': 200}
return response |
Thanks for providing the code. Looking at this, it might be because some required header not being added with the request. Can you try adding client logging to your application? The following log the SDK request signature calculation , the HTTP message sent, and the HTTP response received. This will help investigate this issue further.
|
This issue has not received a response in 1 week. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled. |
Just saw your reply @skotambkar, I will investigate and reply soon. Thank you. |
@skotambkar did you mean var buffer bytes.Buffer
logger := logging.NewStandardLogger(&buffer)
logger.Logf(logging.Debug, "time to %s", "log")
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(rec.AWSRegion),
config.WithLogger(logger),
config.WithEndpointResolver(customResolver),
)
if err != nil {
fmt.Println("cfg err")
}
svc := apigatewaymanagementapi.NewFromConfig(cfg, func(o *apigatewaymanagementapi.Options) {
o.ClientLogMode = aws.LogSigning | aws.LogRequest | aws.LogResponseWithBody
}) but I don't know where it is logging anything, I don't see anything in CloudWatch that wasn't getting logged before. |
Should be apigatewaymanagementapi in your case. The logs would be written to the buffer you passed in with your logger. |
ok I got some logs: read 1024 bytes: SDK 2021/03/18 21:35:50 DEBUG time to log
|
SDK 2021/03/18 21:35:51 DEBUG Response |
I'm encountering the same issue! My JavaScript lambda works fine, my Go one receives a 403. Added debug logging as suggested:
|
@jamessouth is this problem still happening for you? @skotambkar should we re-open this issue since we have two reports of this being an ongoing problem? |
I upgraded the AWS SDK version today and it is just working now 👌 |
Thank you @stephanos @skotambkar version 1.2.1 is working for me! 😊👌😁 |
I seem to be this issue with the current version, did anyone find a fix for this? My response today with apigatewaymanagementapi v1.11.0: Edit: Please ignore this, it was an issue with accessing api gateway from within a vpc. In case anyone ends up with a similar issue: https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-vpc-connections/ |
Describe the question
I'm upgrading SDK from 0.9 to 0.29 and came to issue when I'm sending data to websocket connection.
My old code which is still working with 0.9
New code with 0.29, I'm getting
operation error ApiGatewayManagementApi: PostToConnection, https response error StatusCode: 403, RequestID: <id>, ForbiddenException:
Any idea what I'm doing wrong?
Thanks
The text was updated successfully, but these errors were encountered: