-
Notifications
You must be signed in to change notification settings - Fork 74
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
[Amplify gen2] Receive Amplify Data parameters as environment variables in a Amplify Function #1715
Comments
Hi @antennix, you can grant a function access to the Data API by following these docs: https://docs.amplify.aws/react/build-a-backend/data/customize-authz/grant-lambda-function-access-to-api/ We are also actively investigating making the experience of configuring the data client in the context of a lambda function a better experience, so any feedback you have there is welcome! |
@edwardfoyle On the other hand, is there an approach for when we want to access the data source (Dynamo) directly? For example, executing a daily batch via Function We could update records through AppSync, but I don't think we can do so without users who are executing Subscriptions noticing. I want to update silently. |
Hi, @antennix , I bumped into the same issue a couple of days ago when trying to pass the dynamoDB table name to my lambda hander as an env variable, and I did exact the same thing as you mentioned in the description section, which caused a Then, I joined in a discussion in another similar open issue #2554, where we believe this could be some sort of bug. But fortunately, other great devs came up with some workarounds, which I already tested in my case and they worked just fine. And I hope they could work for you too. Good luck! |
@MyNameIsTakenOMG |
Marking as feature request to provide Data parameters such as table name and arn as environment variables. |
I have solved it by adding this to my backend.ts where Event is one of the resources I want access to
In the stack where I create the lambda function I use:
Inside the lambda you can the use: This works like charm |
I understand the mechanism of preventing circular references by creating Lambda without using DefineFunction. On the other hand, |
my workaround is to:
but now we have a chicken and egg issue because the outputs are artefacts of the build...
some things to note...
as we do text refs we won't have any cloudformation dependencies... idk... hope for an official solution. I am now thinking of using @rpostulart solution now... :) hoping for an official stand :) |
@rpostulart's solution works, but I encountered a significant issue with that approach after pushing to my main branch and attempting to start up my sandbox again: Error:
I resolved it by using a different approach: casting the imported function resource in Here’s a code snippet: const customResourceStack = backend.createStack("ProcessingStack");
export const embeddingQueue = new sqs.Queue(
customResourceStack,
"EmbeddingQueue"
);
// Add IAM and environment variables to the function
const createEmbeddingsFunction =
backend.createEmbeddingsForLaws.resources.lambda as aws_lambda.Function; // CAST HERE!
embeddingQueue.grantSendMessages(createEmbeddingsFunction); // IAM
createEmbeddingsFunction.addEnvironment("SQS_QUEUE_URL", embeddingQueue.queueUrl); // ENV Note: This may cause circular dependency issues, as explained here, but this applies primarily to DynamoDB, not SQS/SNS. Also, when accessing the environment variable in the Lambda function, do not import the |
@pridapablo I got into a similar error but in my scenario I was trying to deploy 2 branches. This is caused by the fact that cloudformation exports space si scoped to the account level For me, the current workaround is to define a secret at the app level and put it in there manually... and before init a local sandbox, ... and after I deploy a new branch. My mentioned solution with importing outputs is also bad, as it only works with a previous deployment. as in: we can't do a new branch from scratch... |
@rpostulart How did you use the native CDK's Function managed out of
|
Without the Lambda resolver's built-in access to the table, the experience becomes much more cumbersome and time-consuming. I now manage the table manually in CDK as follows: const externalTableStack = backend.createStack('ExternalTableStack');
const leagueTable = new Table(externalTableStack, 'League', {
partitionKey: {
name: 'id',
type: AttributeType.STRING
},
billingMode: BillingMode.PAY_PER_REQUEST,
removalPolicy: RemovalPolicy.DESTROY,
});
backend.data.addDynamoDbDataSource(
"ExternalLeagueTableDataSource",
leagueTable
);
leagueTable.grantReadWriteData(backend.leagueHandler.resources.lambda);
(backend.leagueHandler.resources.lambda.node.defaultChild as CfnFunction).addPropertyOverride('LoggingConfig', {
LogFormat: 'JSON',
ApplicationLogLevel: process.env.PRODUCTION ? 'WARN' : 'TRACE',
SystemLogLevel: 'INFO',
});
(backend.leagueHandler.resources.lambda as NodejsFunction).addEnvironment('LEAGUE_TABLE_NAME', leagueTable.tableName); |
Environment information
Description
Is it possible for a Function to receive the Endpoint of Data,
or the name and ARN of Datasources like DynamoDB?
For example, with Storage, you can use the "access" property in the defineStorage function to grant read and write permissions to a Function.
https://docs.amplify.aws/react/build-a-backend/functions/grant-access-to-other-resources/
However, a similar approach cannot be used with defineData as it's not possible to specify properties.
It is possible to directly set values for an already created function in backend.ts using CDK, but
if you're also using defineFunction for resolvers against Data,
I think an error would occur due to circular reference, as the reference order would be resolverFunction → Data → thisFunction.
Is there a good solution?
The text was updated successfully, but these errors were encountered: