에 대한 사용 권한 람다를 만들기 이벤트는 이벤트:PutEvents

0

질문

내가 원하는 람다를 만들 EventBridge 이벤트이지만이 오류를 호출할 때는 람다:

User: arn:aws:sts::120293923901:assumed-role/MyApiOrdersPostFunct-I1QOYC7P1R0Z/MyApiOrdersPostFunct-SJtAeYoiaguW is not authorized to perform: events:PutEvents on resource: arn:aws:events:eu-north-1:120293923901:event-bus/MyApiEventBus because no identity-based policy allows the events:PutEvents action

나는 추가 정책을 하지만 변경합니다.

여기에는 람다를 호출하 eventbridge.


import { APIGatewayProxyHandler, APIGatewayProxyResult } from 'aws-lambda';
import { EventBridgeClient, PutEventsCommand } from '@aws-sdk/client-eventbridge';

const eventBridge = new EventBridgeClient({ region: 'eu-north-1' });

export const post: APIGatewayProxyHandler = async (): Promise<APIGatewayProxyResult> => {
    const event = new PutEventsCommand({
        Entries: [{
            EventBusName: 'MyApiEventBus',
            Source: 'MyApiEventBus.OrderCreated',
            DetailType: 'OrderCreated',
            Detail: JSON.stringify({ description: 'order has been created' }),
        }]
    });
        eventBridge.send(event);

    return {
        statusCode: 200,
        body: '',
    };
};

여기 CDK config. 두 가지 정책(attachInlinePolicy,addToRolePolicy)기 때문에 나는 모두 테스트.

import {
    RestApi,
    DomainName,
    BasePathMapping,
    LambdaIntegration,
    Model,
} from '@aws-cdk/aws-apigateway';
import { EventBus, Rule } from '@aws-cdk/aws-events';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { Policy, PolicyStatement } from '@aws-cdk/aws-iam';

const MyApi = new RestApi(this, `RestApi`, {
  restApiName: 'My API',
  description: 'The My API',
});

// Add an Event Bus
const bus = new EventBus(this, `EventBus`, {
  eventBusName: 'MyApiEventBus',
});

// Add API endpoint
const ordersResource = MyApi.root.addResource('orders');

const ordersPostFunction = new NodejsFunction(this, `OrdersPostFunction`, {
  entry: './lambda.ts',
  handler: 'post',
});

// Allow lambda to create events
ordersPostFunction.addToRolePolicy(
  new PolicyStatement({
    actions: ['events:PutEvents'],
    resources: [bus.eventBusArn],
  }),
);

ordersPostFunction.role?.attachInlinePolicy(
  new Policy(this, `OrdersPostEventBusPolicy`, {
    statements: [
      new PolicyStatement({
        actions: ['events:PutEvents'],
        resources: [bus.eventBusArn],
      }),
    ],
  }),
);

// Role to allow for creating event (not working?)
bus.grantPutEventsTo(ordersPostFunction);

Lambda 역할 문서

{
  "sdkResponseMetadata": null,
  "sdkHttpMetadata": null,
  "partial": false,
  "permissionsBoundary": null,
  "policies": [
    {
      "arn": null,
      "document": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Action": "events:PutEvents",
            "Resource": "arn:aws:events:eu-west-1:120293923901:event-bus/MyApiEventBus",
            "Effect": "Allow"
          }
        ]
      },
      "id": null,
      "name": "MyApiOrdersPostEventBusPolicyACA51C2D",
      "type": "inline"
    },
    {
      "arn": null,
      "document": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Action": "events:PutEvents",
            "Resource": "arn:aws:events:eu-west-1:120293923901:event-bus/MyApiEventBus",
            "Effect": "Allow"
          }
        ]
      },
      "id": null,
      "name": "MyApiOrdersPostFunctionServiceRoleDefaultPolicyE7615F17",
      "type": "inline"
    },
  ]
}
2

최고의 응답

2

이벤트 버스에 위치한 이 eu-west-1 지역으로 표시하여 생성된 정책,하지만 당신은에 액세스하려고서 eu-north-1. 지역을 변경하고 작동합니다.

2021-11-22 15:33:47
1

send 방법 EventBridgeClient 은 async. 그래서 그것을 이어야 한다:

await eventBridge.send(event);

그렇지 않으면 당신은이 통지하지 않을 것에서 발생하는 예외를 이다.

2021-11-22 14:59:02

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................