Povolenia pre lambda chcete vytvoriť udalosť podujatia:PutEvents

0

Otázka

Chcem mať lambda vytvorenie EventBridge udalosti, ale som si to chyba pri volaní lambda:

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

Pridal som politiky, ale žiadna zmena.

Tu je lambda volanie 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: '',
    };
};

Tu je CDK config. Existujú dva politiky (attachInlinePolicy, addToRolePolicy), pretože som skúšal oboje.

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 úlohu dokumentu

{
  "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

Najlepšiu odpoveď

2

Vaša Akcia Autobus sa nachádza v eu-west-1 regiónu, ako ukazuje generované politiky, ale snažíte sa k nemu prístup z eu-north-1. Zmeniť regióne a bude to fungovať.

2021-11-22 15:33:47
1

Na send metóda EventBridgeClient je asynchrónna. Takže by to malo byť:

await eventBridge.send(event);

Inak by ste si to nevšimol výnimky hodil to.

2021-11-22 14:59:02

V iných jazykoch

Táto stránka je v iných jazykoch

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