aws-samples / aws-samples/agents4energy
Wire APPSYNC_HTTP_ENDPOINT (and AGENTCORE_MEMORY_ID) into the AgentCore runtime environment
- Dominant language
- TypeScript
- Stars
- 48
- Forks
- 68
- PR merge metrics
- No merged PRs in 30d
Description
## Background
PR #61 fixed the `appsync:GraphQL` IAM permission for `Mutation.publishAgentEvent`, but flagged a follow-up gap: the AgentCore runtime (`agUiHandlerRuntime` in `web/amplify/backend.ts`) does not currently receive `APPSYNC_HTTP_ENDPOINT` or `AGENTCORE_MEMORY_ID` as environment variables on the primary deploy path.
### `AGENTCORE_MEMORY_ID` — straightforward fix
This one does **not** need a custom resource — it's a construction-order issue. `agUiHandlerRuntime` ([`backend.ts:121`](https://github.com/waltmayf/agentcore-amplify-fullstack/blob/main/web/amplify/backend.ts#L121)) is instantiated in `agentStack` *before* `agentCoreApp` exists ([`backend.ts:161`](https://github.com/waltmayf/agentcore-amplify-fullstack/blob/main/web/amplify/backend.ts#L161)), which is what produces `AGENTCORE_MEMORY_ID` at line 177. Both constructs live in the same stack, so there's no cross-stack token involved. Reordering — build `agentCoreApp` (and compute `AGENTCORE_MEMORY_ID`) first, then pass `environment: { AGENTCORE_MEMORY_ID }` into the `AgentCoreRuntimeWithBuild` props — wires this directly via the construct, no new infrastructure required.
### `APPSYNC_HTTP_ENDPOINT` — real cross-stack constraint
`cfnGraphqlApi` (owning the GraphQL endpoint) lives in Amplify's `data` nested stack, while `agUiHandlerRuntime` lives in `agentStack`. Today, `dataStack` already depends on `agentStack` (its `AgUiHandlerDataSourceRole`/`AgUiHandlerDataSource` reference `agUiHandlerRuntime.runtime.attrAgentRuntimeArn`, see `backend.ts:353-399`). If `agentStack` also referenced `cfnGraphqlApi.attrGraphQlUrl` to build the runtime's env vars, that would make `agentStack` depend on `dataStack` too — a circular nested-stack dependency, which CDK synth rejects.
Options discussed (see full analysis in [PR #61 comments](https://github.com/waltmayf/agentcore-amplify-fullstack/pull/61#issuecomment-4905008146) and [here](https://github.com/waltmayf/agentcore-amplify-fullstack/pull/61#issuecomment-2)):
1. **Co-locate**: construct `agUiHandlerRuntime` (or whatever needs the URL) in `dataStack` instead of `agentStack`, so the env var and the `appsync:GraphQL` IAM grant are both same-stack references — mirrors the classic Lambda+AppSync pattern, no cycle.
2. **SSM Parameter with a fixed name**: have `dataStack` write the GraphQL URL to an SSM Parameter with a deterministic, non-token name; have the runtime read it via `ssm:GetParameter` at startup (or `StringParameter.valueForStringParameter` at synth time). Since the parameter *name* is a plain string, not a CDK token, this sidesteps the stack dependency graph — no cycle, no custom resource.
3. **Custom resource**: an `UpdateAgentRuntime` post-deploy custom resource (imperative SDK call, not a CFN `Ref`) — the original path flagged in PR #61, also cycle-free but adds more moving parts than options 1/2.
## Suggested scope
- Fix `AGENTCORE_MEMORY_ID` wiring via construction reorder (low risk, no architecture change).
- Pick one of options 1–3 for `APPSYNC_HTTP_ENDPOINT` (option 2, SSM parameter, seems like the lightest-weight fix that preserves the current stack split) and implement it.
- Update `docs/ag-ui-handler-pattern.md` to reflect the final wiring approach.
## References
- PR #61: https://github.com/waltmayf/agentcore-amplify-fullstack/pull/61
- Relevant code: `web/amplify/backend.ts` (agentStack/dataStack split, `agUiHandlerRuntime`, `cfnGraphqlApi`, `AgUiHandlerDataSourceRole`/`AgUiHandlerPublishEventPolicy`)
---
*Imported from `waltmayf/agentcore-amplify-fullstack`#64 — originally filed by @app/github-actions on 2026-07-07. Migrated to open-source repo; cross-references updated post-import.*
Contributor guide
Research direction
Read web/amplify/backend.ts, especially the agUiHandlerRuntime, agentCoreApp, cfnGraphqlApi, and data-source sections around lines 121, 161, and 353-399. Review the linked PR #61 comments before choosing among co-location, SSM, or a custom resource. Done means both environment variables reach the AgentCore runtime without a nested-stack cycle and docs/ag-ui-handler-pattern.md describes the final wiring.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, documentation, infrastructure
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100