serverless-offline icon indicating copy to clipboard operation
serverless-offline copied to clipboard

SLS_ACCOUNT_ID identity override for schedule events

Open Jengah opened this issue 1 year ago • 0 comments

Feature Request

SLS_ACCOUNT_ID and related env var overrides only work for http event types and not schedule events. The AccountID is an identifier that would be useful for testing cross-account Cloudwatch events triggered by rate or cron schedules.

This logic appears to live at src/events/schedule/ScheduleEvent.js, but my feeble attempts to get this working myself have thus far failed. I though it would be as simple as adding a check for the variable and importing env (see sample below), but so far have come up empty-handed, with the auto-generated id still being used.

Sample Code

  • file: serverless.yml
service: my-service

plugins:
  - serverless-offline

provider:
  runtime: go1.x
  stage: local

functions:
  hello:
   environment:
     SLS_ACCOUNT_ID: "XXXXXXXXXXXXXXXXXXXX"
   events:
      -  schedule: rate(1 minute)
   handler: bin/hello
  • file: src/events/schedule/ScheduleEvent.js
import { createUniqueId } from '../../utils/index.js'
import { env } from 'node:process'

export default class ScheduleEvent {
  account = env.SLS_ACCOUNT_ID || createUniqueId()

  ['detail-type'] = 'Scheduled Event'

  id =  createUniqueId()

  region = null
}

Expected behavior/code

The presence of env.SLS_ACCOUNT_ID should be used as account rather than createUniqueId()

Additional context/Screenshots

Jengah avatar Oct 02 '23 16:10 Jengah