Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reference a DynamoDB table name and Elasticsearch domain endpoint in a Lambda function CF template? #2613

Closed
alex-vladut opened this issue Oct 21, 2019 · 6 comments
Assignees
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 question General question

Comments

@alex-vladut
Copy link
Contributor

alex-vladut commented Oct 21, 2019

Note: If your question is regarding the AWS Amplify Console service, please log it in the
official AWS Amplify Console forum

Which Category is your question related to?
In a Lambda function I would like to reference a DynamoDB table name and the Elasticsearch endpoint created as part of the API (GraphQL) stack. Preferably I would like those values to be provided as environment variables.

What AWS Services are you utilizing?
AppSync, Lambda, DynamoDB, Elasticsearch

Provide additional details e.g. code snippets
I created a new Lambda function by using the command amplify add function. The CloudFormation template generated by Amplify CLI for such a Lambda function includes a single Parameter env:

	"Parameters": {
		"env": {
			"Type": "String"
		}
	}

How can I reference as well a DynamoDB table name created for the GraphQL/AppSync schema and the Elasticsearch domain endpoint created as a result of making the entity @searchable? Is there any documentation on how that could be accomplished?

@alex-vladut alex-vladut changed the title How to reference a DynamoDB table and Elasticsearch endpoint in a Lambda function CF template? How to reference a DynamoDB table name and Elasticsearch domain endpoint in a Lambda function CF template? Oct 21, 2019
@kaustavghosh06
Copy link
Contributor

Currently we allow trigger/streaming functionality for S3 and DDB tables added using the amplify add storage functionality.
This PR - #2463 enables you to add triggers/streaming to the DDB tables provisioned as a part of the GraphQL transformer as well. Maybe this is what you're looking for?

@kaustavghosh06 kaustavghosh06 added question General question pending-response Issue is pending response from the issue author labels Oct 21, 2019
@alex-vladut
Copy link
Contributor Author

alex-vladut commented Oct 22, 2019

Thank you for reply 👍.
A specific use case I have in mind is to have a Cognito Post-Authentication Lambda Trigger (i.e. whenever a new user signed up) and as a result to update a User's details in DynamoDB. So in that case I would need the table name generated as part of the GraphQL schema (User being defined into schema.graphql), named something like "User-AppSyncId-env". For the moment what I did was to hard-code it, but I wanted to find a more generic way so that I don't have to manually change it if I want to redeploy the application from scratch. For reference, here is the Lambda function I am playing with:
https://github.com/alex-vladut/challenge-me/tree/master/amplify/backend/function/challengemereacta7d0cb31PostConfirmation

Regarding referencing the Elasticsearch endpoint/domain, I wanted to integrate geolocation search and as a result I followed the guidelines here:
https://aws-amplify.github.io/docs/cli-toolchain/graphql#add-a-custom-resolver-that-targets-an-aws-lambda-function
Instead of adding the geo_point mapping manually I wanted to create a Lambda function so that I could easily perform this action in the future if needed. Unfortunately I wasn't able to find a way to programatically provide the Elasticsearch domain and endpoint into the Lambda function's CloudFormation template so that could be further forwarded as environment variables to it.

Thanks again for your help, I hope that explains better what is my use-case here.

@mrducky4
Copy link

I think I did something similar. I have a graphQL API, and a REST API. The REST API uses a lambda function which amplify created. When I added the REST API, I answered a question saying I wanted the function to have access to the appsync stuff (not really understanding how this would work). Amplify created a cloudformation-template.json file for the new function, which had a section in it to provide the permissions for the lambda function. But it did not give permission to directly access the DynamoDB tables used in the graphQL API. So I added that manually in the new template, in the existing AmplifyResourcesPolicy section. I added a new section like this, copying the format of existing sections:

{
	"Effect": "Allow",
	"Action": [
		"dynamodb:BatchGetItem",
		"dynamodb:GetItem",
		"dynamodb:Query",
		"dynamodb:Scan",
		"dynamodb:BatchWriteItem",
		"dynamodb:PutItem",
		"dynamodb:UpdateItem",
		"dynamodb:DeleteItem"
	],
	"Resource": [
		{
			"Fn::Join": [
				"",
				[
					"arn:aws:dynamodb:",
					{
						"Ref": "AWS::Region"
					},
					":",
					{
						"Ref": "AWS::AccountId"
					},
					":table/Site-",
					{
						"Ref": "apimyapinameGraphQLAPIIdOutput"
					},
					"-",
					{
						"Ref": "env"
					}
				]
			]
		}
	]
},

After amplify deploys the lambda function, I can go to the AWS Lambda console and see the permissions given to the function to verify it is what I intended.

@kaustavghosh06 kaustavghosh06 self-assigned this Oct 28, 2019
@kaustavghosh06 kaustavghosh06 added graphql-transformer-v1 Issue related to GraphQL Transformer v1 and removed pending-response Issue is pending response from the issue author labels Oct 28, 2019
@alex-vladut
Copy link
Contributor Author

Thanks a lot for your time. Your suggestion above will work fine for DynamoDB, but not sure if the same pattern will apply as well for Elasticsearch resources. In addition to that, in my opinion this is still not the right approach as the CF stacks referencing a DynamoDB table in this way will know how its name is built. If the table name format changes in the future, then all the places referencing a DynamoDB table will have to be adjusted as well.
Anyway, it looks like there is no clear solution for this issue at the moment. You can close this ticket if you wish, I'll keep digging when I have some time, maybe I find something.

@ctrlplusb
Copy link

@mrducky4 your work wouldn't perhaps be open source would it? I am looking to do similar. 😇

@github-actions
Copy link

github-actions bot commented Jun 9, 2021

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 question General question
Projects
None yet
Development

No branches or pull requests

4 participants