如何让AWS AppSync GraphQL解析器对接CodeStar生成的Lambda REST端点?
Absolutely! You can absolutely have your AWS AppSync GraphQL resolvers pull data from the REST API endpoints exposed by your AWS CodeStar-generated Lambda functions—you don’t need to directly invoke the Lambda functions themselves. This is a great way to reuse your existing REST infrastructure while adding a GraphQL layer on top. Here’s a step-by-step breakdown of how to set this up:
1. Create an HTTP Data Source in AppSync
First, you’ll need to set up an HTTP data source in AppSync that points to your CodeStar-generated REST API base URL (e.g., https://my-prod-example-url):
- Go to your AppSync console, navigate to Data Sources, click Create data source.
- Choose HTTP as the data source type.
- Enter a name for your data source (like
CodeStarRESTAPI), then input your REST API’s base URL in the Endpoint field. - Configure authentication if your REST API requires it:
- If using API Key auth, add the key to the Headers section (you can hardcode it or pass it via resolver arguments).
- If using IAM auth, select AWS IAM as the authorization type and ensure the AppSync service role has permissions to invoke your REST API.
2. Configure Resolver Mapping Templates
Next, link your GraphQL fields (like fetchPosts) to this HTTP data source using VTL (Velocity Template Language) mapping templates.
Example: Resolver for fetchPosts
Request Mapping Template (VTL)
This template tells AppSync to send a GET request to the /posts endpoint of your REST API:
{ "version": "2018-05-29", "method": "GET", "resourcePath": "/posts", "params": { "headers": { "Content-Type": "application/json" # Uncomment below if your API uses an API key # "x-api-key": "$context.arguments.apiKey" } } }
Response Mapping Template (VTL)
This template converts the REST API’s JSON response into the format expected by your GraphQL schema, with basic error handling:
#if($context.result.statusCode == 200) $util.toJson($context.result.body) #else $util.error("Failed to fetch posts: $context.result.body", "REST_API_ERROR") #end
Key Considerations
- Endpoint Reusability: If your REST API has multiple endpoints (e.g.,
/posts,/users,/comments), you can either create separate HTTP data sources for each, or reuse a single data source and dynamically set theresourcePathin each resolver’s request template. - Security: Ensure your REST API’s security groups or access policies allow traffic from AppSync. If your API is in a VPC, you’ll need to configure VPC access for your AppSync API.
- Error Handling: Extend the response template to handle different HTTP status codes (404, 500, etc.) and map them to meaningful GraphQL errors for your clients.
- Performance: Consider adding caching in AppSync for frequently accessed fields to reduce redundant calls to your REST API.
This approach lets you leverage your existing CodeStar REST API endpoints directly from AppSync resolvers, no need to refactor your Lambda functions to work with direct AppSync invocations.
内容的提问来源于stack exchange,提问作者Adhy Wiranata

