AWS AppSync: No option to add Lambda resolver - amazon-web-services

I have created an AppSync API and a lambda data source and a resolver to provide data for it. I managed to successfully run queries a couple of days ago.
I wanted to attach a second lambda resolver for a different GraphQL query. I added a new lambda as a data source, but when I click Attach in the schema next to the query, I am forwarded to the page for creating a pipeline resolver and there is no way to choose a lambda resolver instead.
Now, even when I just create a new copy of the previous AppSync API with one query and want to attach a lambda resolver to that single query, there is no way to attach a lambda resolver any more. Though it was possible earlier this week. And I can see on old APIs that they are still using lambda resolvers.
Has anything changed on AppSync recently? Or how can I attach a lambda resolver to a query, not a pipeline resolver?

When the pipeline page opens you select the "Actions" dropdown menu, and then you can choose the "Update runtime" option. Under resolver type you can then choose a Unit type resolver which you are probably looking for.
Something did change, there is a new feature where you can write resolvers using JavaScript and i guess they've put a PIPELINE resolver as the default one.

Related

Invoke Lambda function on button click in React

To start I just want to say that I'm really new to AWS overall (1-2 weeks). So I might not think correct from start.
I'm working on an app where I want to click a button in JSX and trigger a Lambda function that gets data from a dynamodb table and shows it in the UI.
I'm writing the infrastructure as code using aws cdk. How should I face this? Do you have any guides that I can follow?
Welcome! Please have a look on this guide.
With AWS, it's a best practice to create an AWS API Gateway, which acts as your HTTP/HTTPS/API web server.
Within this API gateway, you can integrate a Lambda function with a route (e.g. GET /users), where that Lambda function grabs the data from AWS DynamoDB process it as you wish and returns the output.
To integrate it with your code, you create an HTTP GET/POST request to the API gateway URL.

How to call an API Gateway API once a week

I have a REST API built using API Gateway with a couple of methods. I need to run a POST request on a method /generate-stats once a week. I currently call this method through the AWS console by pasting a request body into the "Test" feature that exists in API Gateway under the Method Execution flowchart.
How would I go about automating this call? Would a lambda that runs once a week be the simplest solution? Ideally I can store the response or trigger an alarm if the request fails.
If you want to automate a request to happen once a week you would want to look at using Amazon EventBridge.
The service itself supports either being triggered by an event (such as a new PutObject into S3 or an instance being launched) or can run based on a schedule. You would want to use the latter to set a cron expression for running this.
The next part of the rule is the target which in this case are a couple of approaches.
API Gateway requests are a supported target from within the event. If the supported functionality with EventBridge is suitable for you then you will be able to perform the request directly without any additional services.
If additional functionality is required you would need to create a Lambda function that could perform the request to API Gateway. This Lambda would then be the trigger for the event leading to the same functionality being performed.
You can build a Lambda function that can use code to perform a POST request. Then you can use scheduled events to schedule when the Lambda function will be invoked. Using a CRON expression, you can schedule your Lambda to fire once a week. For details, see:
Schedule AWS Lambda Functions Using CloudWatch Events

aws appsync environment variable for api key in lambda function

I have react web app that uses aws amplify to build its backend. This backend consists of a graphql api with a dynamodb table as data source (created by the model declared in my graphql schema) and a lambda function that fetches information from a 3rd party api and saves the results into the dynamodb table to make it available for the app.
Following this tutorial:
https://docs.amplify.aws/cli/function#graphql-from-lambda
I created the lambda function running the "amplify add function" command. I answered "yes" when I was asked if the lambda should have access to other amplify resources and I gave the lambda write and update access to my graphql api, so that I can perform mutations from the lambda.
When I finish the creation process, amplify informs me that I have two new environment variables available in the lambda:
API_<MY_APP>_GRAPHQLAPIENDPOINTOUTPUT
API_<MY_APP>_GRAPHQLAPIIDOUTPUT
The problem is, however, that I also need the api key in order to be able to perform the mutations (my graphql api has "api key" as primary id method).
The tutorial makes use of process.env.API_KEY. However they don't explain where this variable comes from, as in my case its value is undefined.
Do you know how to make the api key available to my lambda?
P.S.: Hardcoding is not an option, as I have several backend environments.
The env var is called API_<APP_NAME>_GRAPHQLAPIKEYOUTPUT. It appears only if you enable API KEY authentication for your GraphQL API.
Here is an example of what is printed into the console when you add access permission for a Lambda function to access a GraphQL API:
You can access the following resource attributes as environment variables from your Lambda function
API_AMPLIFYTEST_GRAPHQLAPIENDPOINTOUTPUT
API_AMPLIFYTEST_GRAPHQLAPIIDOUTPUT
API_AMPLIFYTEST_GRAPHQLAPIKEYOUTPUT
Also, the Amplify CLI generates a comment on top of your Lambda function index file with the list of useful env vars.
I tested this with Amplify CLI version 4.36.0.

Creating AWS AppSync using AWS Lambda (NodeJS)

In my project, we create tables in AWS dynamodb based on the user id using AWS lambda (NodeJS).
Once the table is created the lambda function will create a graphql endpoint for that particular table so that the user can get real-time data for that specific table.
Can we create graphql endpoints which will also be seen in AWS appsync from AWS lambda (NodeJs)?
I searched the web but didn't find any tutorial from AWS nor any articles regarding it.
Well to be frank its will be very hard to achieve such functionality but if you still required then yes you can do that using AWS AppSync SDK
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/AppSync.html
From you tags assuming that you are using javascript.
Just my suggestion :- Dont try to do such things as it wont be easy to manage multiple app async and cost will increased x10

AWS API Gateway: How to achieve continuous delivery?

I'm building an API using AWS API Gateway and AWS Lambda. I would like to achieve continuous delivery for this API. The path I've chosen to do it is to use CloudFormation through AWS CodePipeline. I've managed to to it for another project using Lambdas (without API Gateway), it works perfectly and it is really pleasant to use.
The issue I'm facing when deploying is that the Lambdas are properly updated but not the API definition. From what I understand, the AWS::ApiGateway::Deployment are immutable resources which means that for each deployment of the API I need to create a new AWS::ApiGateway::Deployment resource. This is not practical at all because for each of this AWS::ApiGateway::Deployment I have a new Invoke URL. This is not acceptable since I would have to either change my DNS record to the newly deployed API invoke URL or ask our API users to change the URL in their applications.
What I would like is to be able to change the API definition and the Lambdas implementations without my API users having to change anything in their applications.
How can I achieve this behavior?
I created a tutorial to highlight my issue. You can find it at: https://github.com/JonathanGailliez/aws-api-gateway-lambda-example
As per: https://forums.aws.amazon.com/thread.jspa?messageID=789869&#789869
joey-aws says:
We are currently in the process of rolling out a solution which
addresses this exact problem. In the meantime, a common workaround
would be to update something small, such as a "description" field
which could then be used to "trigger" an API Gateway deployment when
updating the CloudFormation stack.
I'll update this answer and the example repo once it's rolled out.
You could run a Cloudformation update from the command line or in the AWS console. This would change the API definitions and any lambda code without changing the unique id to access your gateway.
The other option is to put your API behind a custom domain name and then you could keep deploy a new API or stage and switch over the custom domain mapping when you are ready. The users wouldn't recognize any change.
A way to achieve that is by leveraging existing frameworks like
AWS SAM
Serverless
Claudia
I was able to achieve this by using CloudFormation template generated by troposphere and boto3 api in Python as follows:
Split the template into two parts
API definition, Method(s), IAM roles, ApiKey and Lambda (a)
Deployment, UsagePlan and UsagePlanKey (b)
Once changed Lambda code is zipped up and uploaded to S3 using boto3 api
Stack (b) is deleted
Stack (a) is updated with new resource id for the GET method connected to lambda
Stack (b) is created anew
Steps 3, 4, 5 are performed using CloudFormation boto3 api with blocking until completed.
Most importantly after all steps are done ApiKey value and stage Invoke URL remain the same, running updated Lambda code as tested with curl.
Note: it may take additional 30-60s for API to become fully functional after CloudFormation update is completed.