Hello, I am running into `A version for this Lambd...
# help
r
Hello, I am running into
A version for this Lambda function exists ( 1 ). Modify the function to create a new version.
on a stack using Cognito, SSM and Next.js. It seems like
ApiFunctionCurrentVersion
ImageFunctionCurrentVersion
are failing to create and and
MainFunction
is failing to update. Could this mean something’s wrong with my constructs? I also added
"@aws-cdk/aws-lambda:recognizeVersionProps": true
to my
cdk.contect.json
.
t
can I see the function definition code?
r
Do you mean this?
Copy code
export default class LunaAppNextJsStack extends sst.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);
    const site = new sst.NextjsSite(this, "LunaAppSite", {
      path: "frontend",
      environment: {
        // Pass the table details to our app
        REGION: scope.region,
        NEXT_PUBLIC_USERPOOLID: props.UserPoolId,
        NEXT_PUBLIC_IDENTITYPOOLID: props.IdentityPoolId,
        NEXT_PUBLIC_USERPOOLCLIENTID: props.UserPoolClientId,
      },
    });

    // Show the site URL in the output
    this.addOutputs({
      URL: site.url,
    });
  }
}
t
Oh this is for nextjs specifically, will let @Frank handle this one
r
I am currently migrating over to SST from CDK, but I do not think there’s any other resource clashing with the resource names or similar.
Here’s the part of the logs that’s failing:
Copy code
prod-luna-app-luna-app-stack | UPDATE_IN_PROGRESS | AWS::Lambda::Function | LunaAppSiteMainFunction30DEEBA3
prod-luna-app-luna-app-stack | CREATE_IN_PROGRESS | AWS::Lambda::Version | LunaAppSiteImageFunctionCurrentVersion1D7E26DBe01ed0a82e7d4ce23048ae416ee4a604
prod-luna-app-luna-app-stack | CREATE_IN_PROGRESS | AWS::Lambda::Version | LunaAppSiteApiFunctionCurrentVersion6A272B5293520a1ea237ad659617b54099eaeb0a
prod-luna-app-luna-app-stack | CREATE_FAILED | AWS::Lambda::Version | LunaAppSiteApiFunctionCurrentVersion6A272B5293520a1ea237ad659617b54099eaeb0a | A version for this Lambda function exists ( 1 ). Modify the function to create a new version.
prod-luna-app-luna-app-stack | CREATE_FAILED | AWS::Lambda::Version | LunaAppSiteImageFunctionCurrentVersion1D7E26DBe01ed0a82e7d4ce23048ae416ee4a604 | A version for this Lambda function exists ( 1 ). Modify the function to create a new version.
prod-luna-app-luna-app-stack | UPDATE_FAILED | AWS::Lambda::Function | LunaAppSiteMainFunction30DEEBA3 | Resource update cancelled
Probably also worth mentioning I am currently deploying locally, by running
npx sst deploy --stage prod
Hmm.. more on this: I tried to delete my sst resources, but got the following errors. It seems like I might have messed up something while doing some initial deploys and changing ids.
Copy code
prod-luna-app-luna-app-stack | DELETE_FAILED | AWS::Lambda::Function | LunaAppSiteImageFunctionD45E7A54 | Resource handler returned message: "Lambda was unable to delete arn:aws:lambda:us-east-1:661269018010:function:prod-luna-app-luna-app-st-LunaAppSiteImageFunction-WFVs6hVvXuOz:1 because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas. (Service: Lambda, Status Code: 400, Request ID: 9b03f919-9d75-49fd-b750-a81f66c0dc34, Extended Request ID: null)" (RequestToken: ed4509c9-4ca6-4563-ea2e-c3ecf2ccfe20, HandlerErrorCode: InvalidRequest)
prod-luna-app-luna-app-stack | DELETE_COMPLETE | AWS::CloudFront::CachePolicy | LunaAppSiteImageCacheECC50F5E
prod-luna-app-luna-app-stack | DELETE_IN_PROGRESS | Custom::SSTBucketDeployment | LunaAppSiteS3Deployment9F048E0B
prod-luna-app-luna-app-stack | DELETE_FAILED | AWS::Lambda::Function | LunaAppSiteMainFunction30DEEBA3 | Resource handler returned message: "Lambda was unable to delete arn:aws:lambda:us-east-1:661269018010:function:prod-luna-app-luna-app-st-LunaAppSiteMainFunction3-zmr8LxrlkISV:4 because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas. (Service: Lambda, Status Code: 400, Request ID: 5e47eec3-aa3c-43fd-a590-0cb6cf498e7e, Extended Request ID: null)" (RequestToken: 64d91037-adab-f645-d3bf-bcd191c81df0, HandlerErrorCode: InvalidRequest)
prod-luna-app-luna-app-stack | DELETE_IN_PROGRESS | AWS::Lambda::Function | LunaAppSiteApiFunction66384D5B
Checking remove status...
prod-luna-app-luna-app-stack | DELETE_COMPLETE | AWS::CloudFormation::CustomResource | LunaAppSiteCertificateCertificateRequestorResourceA52E078A
prod-luna-app-luna-app-stack | DELETE_IN_PROGRESS | Custom::SSTLambdaCodeUpdater | LunaAppSiteMainLambdaCodeReplacerA502E313
prod-luna-app-luna-app-stack | DELETE_FAILED | AWS::Lambda::Function | LunaAppSiteApiFunction66384D5B | Resource handler returned message: "Lambda was unable to delete arn:aws:lambda:us-east-1:661269018010:function:prod-luna-app-luna-app-st-LunaAppSiteApiFunction66-Ml2XCyo1dr1I:1 because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas. (Service: Lambda, Status Code: 400, Request ID: 0c0a3e39-dae7-4b73-87bb-2b9e49ecbd49, Extended Request ID: null)" (RequestToken: a577c97b-4ffe-69f1-e210-5f094c5a978d, HandlerErrorCode: InvalidRequest)
b
I am getting this error too from
NextjsSite
construct, this happened after I change logRetention for all functions in
app
construct using
app.setDefaultFunctionProps
. and I don't want to remove my stacks, especially in prod environment
@Roberto Novelo BTW, if you changed your stack code and want to remove, I think you need to remove them from cloudformation
r
I am currently removing the lambda@edge functions manually in the console, I am also using
setDefaultFunctionProps
to set the runtime to
nodejs14.x
, I wonder if this is related.
Weirdly enough, all of these lambdas show up with a
Node.js 12.x
runtime in my lambdas console UI.
b
I think it's easier for you to remove from cloudformation. I am using set runtime too, and actually it's not causing a problem
Maybe
NextjsSite
is using 12.x runtime
r
Deleting the stack in cloudformation is failing due to these lambda@edge replicated functions (main function, images function and api function), I am still figuring out how to remove these resources atm 🤔 .
I was able to delete the stacks after deleting the original functions ✔️ . I will try to find a way to reproduce this issue. I am pretty sure it was caused by renaming ids while testing initial deploys
I can confirm that I am no longer seeing this error. This must have been an edge case when renaming stack IDs.
f
Hey @Roberto Novelo @Bshr Ramadan I just did some digging and wanted to shared what I found. TLDR; it’s a bug in how CDK generated Lambda versions, and it should’ve been fixed https://github.com/aws/aws-cdk/issues/17810. To add some context, CDK added a metadata field to the Lambda Function resource. It used to look like this:
Copy code
"Metadata": {
 "aws:cdk:path": "...",
 "aws:asset:path": "...",
 "aws:asset:property": "Code"
}
And later they added a new field:
Copy code
"Metadata": {
 "aws:cdk:path": "...",
 "aws:asset:path": "...",
 "aws:asset:is-bundled": false,
 "aws:asset:property": "Code"
}
CDK thinks the Lambda function changed, and generated a new version. But since the Lambda function’s code/config didn’t really change, AWS Lambda rejected the deploy with the error
A version for this Lambda function exists
According to the GitHub issue, this should’ve been fixed.
b
Yes I think it had been resolved after update to cdk v2 thank you @Frank
r
Wow I really appreciate it that you kept this in mind. Thank you guys!
Weirdly enough, I am getting this error while removing a stack from a fresh account. Getting this error for all the Nextjs site's lambda@edge functions. (Main,Image...etc)
Copy code
prod-luna-dashboard-nextjs-stack failed: Resource handler returned message: "Lambda was unable to delete arn:aws:lambda:us-east-1:redacted:function:prod-luna-dashboard-nextj-LunaDashboardNextJSMainF-3HNO9FLGlJST:1 because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas. (Service: Lambda, Status Code: 400, Request ID: e8fca788-4110-416e-9cb3-5671a47783a0, Extended Request ID: null)" (RequestToken: 45c617c4-b7e3-180c-a42c-ec9c1bf0526e, HandlerErrorCode: InvalidRequest)
I think this might be happening because the CF distribution's being deleted before the lambda@edge functions, IDK if it is removing the behavior before deleting the distribution or if that would cause a problem trying to delete the lambda function as well.
f
Hey @Roberto Novelo, yeah it’s a limitation of Lambda@Edge functions cannot be removed directly.
If you wait for some time (ie. 1hr) and try removing the stack again, it should work.
There have been some discussion on how to handle this in SST out of the box https://github.com/serverless-stack/serverless-stack/issues/835
I will keep you posted once we setting on a solution.
r
I am not sure if recent releases have some work on this, but I removing stacks has been working since I last updated sst a couple days ago,
f
We havent’ fixed this yet. Removing might or might not work depending on how fast the replicas are deleted at the edge locations on the AWS side.
Usually if you retry removing a stack after half an hour or so, it will work
r
that makes sense, the next.js sites have been deploying but there has been no hits on the urls so probably none have been created, and thus the nextjs sites delete immediately.
f
Ahh that makes sense
r
I am more and more leaning towards switching to lb + fargate. I have been hosting some sites this way and I get more freedom and also the latest features
I wonder if anyone's got next.js working with lambda layers as well 🤔
f
Oh with fargate, are you running it as a normal server, ie. using
npm start
?
r
yes, I am running
server.js
with
node
, generated by setting
outputStandalone:true
in next experimental config. I am serving a version of vercel's platforms example in github for this other use case, but I am strongly leaning towards switching to this as well for my main site.
I'd love it if there was a Next.js construct version of this, I migrated my RDS stack and have been loving using the client in the UI. You guys are killing it!
f
Ah got it! Thanks for the details. I opened an issue here to track this https://github.com/serverless-stack/serverless-stack/issues/1475
Will bump up the priority on this if more ppl request this 🙌
c
@Frank I messaged on an older thread but this seems more up to date. Just curious if there has been any movement on the clean up of edge functions