Hey guys, did you every try to configure Amplify i...
# help
m
Hey guys, did you every try to configure Amplify inside a Stack? (I need it in order to Auth.SignUp() user when I add him to my DB). Is there any workaround?
g
I think Amplify only works in the browser.
You may take a look at the AWS-SDK Cognito module. You can signUp a user via API calls after you are done with your thing.
m
Hey Gabriel, Thing is, this works if I hardcode the values it needs, it Signs up the user that I added via API call and everything
It's only that I want to pass those values properly
r
Yeah, I think the desire here is to be able to pipe the values for
userPoolId
,
identityPoolId
etc that are output by the stack into the frontend configuration of Amplify.
t
Pass them as environment variables from cognito to your frontend 🙂 https://docs.serverless-stack.com/packages/static-site-env
m
Hey Thomas, I already did that for the frontend it works fine, I need it for the backend tho 🙂
t
ooh user sign up on the lambda?
m
Yes
t
You can expose properties from your class by defining them outside the constructor and use them in other stacks
but I would normally configure Amplify in my react/client side js code not in my stack, I don’t know if that is possible or works?
m
Yes Amplify is meant for the Frontend, but I thought since its all a Node.js app, it should be able to work in the backend, and it does work with hardcoded values, but somehow when I pass in the values I need into Amplify.configure() and console log the result, I get undefined as values
t
ahh okay, so the stack here is not the backend, just configuration as code
m
It's a part of Frontend Stack that passes into all the props I need for the Frontend, so it is a backend
t
The stacks are just a high level abstraction around aws cloudformation. unfortunately It’s not possible to do it the way you are trying. They just describe resources to create in aws
c
The question here is, as @Ross Coundon nicely pointed out, how to get the
userPoolId
,
identityPoolId,
etc from the Lambda handler. The variables are available in the env for the frontend. How do we get them from the Lambda handler? Besides that, everything works since we are able to SignUp the user when we hardcode the values for the Amplify config
g
To elaborate on what Thomas and I are trying to say: • you're trying to call amplify inside an INFRA construct. This code runs at build time. And it builds cloud formation that is sent to AWS, and the infra is created. • Cloud formation has a special thing called references. It references a "future resource." Since AWS needs to deploy/creates it before having the actual ID. • It looks like you're mixing infra code and application code. • You can call Cognito using its SDK to register a user after you did something in your DB (maybe via a lambda). Or maybe amplify. But I think it won't run on a lambda. • If you're trying to create a user after the deployment, maybe a script is the best place to do it. https://docs.serverless-stack.com/constructs/Script • From the screenshot
Amplify.config
part doesn't run on a lambda. It runs when you're building your frontend infra definition. Because it's inside FrontendStack. It isn't a lambda. And that's why you can't get the actual value. Because you're working with INFRA definition. The env is available for the Frontend(react project) because the outputted cloud formation can handle future reference. •
auth.cognitoUserPool.userPoolId
may not exists because aws did not create it when you run Amplify.config. It's like a chicken-egg problem. • It works when you hardcoded the values because the resource already exists and you put the values there. But this is not always the case when you are deploying the first time/working with cdk (infra definition) • I recommend trying to identify what is infra as code and what is runtime code (application logic). And not mix it. This is how I understand your question. Sorry if I got it wrong. I hope it helps. Need to go now... see you
d
Just to throw out there, I have used Amplify in the CDK, but even AWS support recommended not doing so. There are tons of shortcomings since Amplify is so magical, and redeploying the Stack overwrites the magic. The guys above are giving good advice.
c
Thanks @Gabriel Araújo for your detailed answer. What we are trying to do is to SignUp a user via API call. We need to be able to POST the user info to a certain endpoint, and SignUp them (insert them into UserPool) + store some more data to our database. What would be the best way to do this? We are using the following to SignUp the user:
const result = await Auth.signUp({
username: cellphone_number,
password,
})
g
You can use the API construct to deploy a lambda/Endpoint: https://docs.serverless-stack.com/api In your lambda handler (the function you put in the source file. E.g.
src/create.main
) You can get the HTTP params. Store it in your database. And call Cognito. • Im not sure if amplify will run on lambda code. • What I recommend is using AWS SDK Cognito to call Cognito APIs and creates the user. You can find the documentation here for v3: ◦ Homepage: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cognito-identity-provider/index.html ◦ SingUp an user: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cognito-identity-provider/classes/signupcommand.html ◦ Admin Confirm SignUp an user if you want to confirm it: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cognito-identity-provider/classes/adminconfirmsignupcommand.html • You can pass the cognito ids to use it in the lambda handlers via environments variable: like here https://docs.serverless-stack.com/constructs/Api#specifying-function-props-for-all-the-routes You'll then have an endpoint to call e.g: "`https://{restapi_id}.execute-api.{region}.amazonaws.com/{stage_name}/my-endpoint-address`" That can perform what you defined in the handler file.
c
@Gabriel Araújo Thanks again! Really great resources and help!