Looks like the CDK CustomResource changed since I ...
# help
d
Looks like the CDK CustomResource changed since I last used it. I things like the CustomResource Provider Framework… that reads like it handles a ton for you. For instance, a lot of examples show to implement the CR functions as
lambda.SingletonFunction
which makes a ton of sense if you are building a construct that could be used a number of times in the same stack…. you would not want it to reimplement the CR function as many times. However, I’d like to be able to use the same asset compression for functions that
sst.Function
gives you. Am I missing something, is there an easy way to have my cake and eat it too?
t
Have you considered using
sst.Script
?
d
do these run outside the main stack? I’m pretty sure my usecase is a CustomResource
needs to support full CUD
t
sst.Script
is built on top of CustomResource. You can pass in handlers for CUD
t
We could probably use it. It might be overkill in some places and I believe our NextJS implementation is based on another library so that might be why we directly used CustomResource
d
I guess the more important question, is why
sst.Script
and not
*sst*.CustomResource
or
cdk.CustomResource
what makes Script easier/better
its not clear from the docs
t
There's probably two reasons to use sst.Script 1. You can use sst.Function features, like bundling etc. 1. The api is simpler It's not too dissimilar from our other constructs like
sst.Queue
where we just provide a nicer layer on top of the raw cdk Construct
d
forgive my nit… but why call is Script and not just keep it as
CustomResource
? Like you did with
Bucket
or
App
?
t
I think @Frank was behind the naming - not sure will let him chime in. I'd guess that our users were asking us "how to run scripts after deployment" so we figured people without any cdk knowledge were looking for the concept of a script
d
fair enough. Does Script offer the
lambda.Singleton
feature, which I’m not even sure how it works but it seems to make sense for a utils CR that you would not want/need to be replicated multiple times.
t
I didn't know about LambdaSingleton until you brought it up so I'm not sure
d
heh… I only just read about it now as well. but it sounds like a good idea if you are creating a construct that implements a CR and that construct could be used multiple times in the same stack. That would net out to redundant CRs being created. An example is the S3Bucket autoDelete CR.
Also, do you know anything about https://docs.serverless-stack.com/constructs/Script#upgrading-to-v0460?
If you are configuring the 
function
 like below, 
function
 gets run both when the 
Script
 is creating, and each time the SST app is deployed.
But if you implement as onCreate, onUpdate, etc… then it ONLY runs on those, and NOT every deploy, yes?
t
Yeah we broke up the single handler into multiple handlers for specific events
Yeah that maybe needs better wording
d
sounds like its trivial to implement a SingletonFunction…. so I suppose I will be able to use Scripts
@thdxr Ya looks like the @aws-cdk/custom-resources cr.Provider is a pretty simple implementation (could be simpler) but it does a bit more than Scripts and I think I can still use it with
sst.Function
• Handles responses to AWS CloudFormation and protects against blocked deployments • Validates handler return values to help with correct handler implementation • Supports asynchronous handlers to enable operations that require a long waiting period for a resource, which can exceed the AWS Lambda timeout • Implements default behavior for physical resource IDs.
Tried implementing the core CustomResource Provider  but using
sst.Function
for the function and getting what I think is a building issue when it executes…
Received response status [FAILED] from custom resource. Message returned: Upe is not a function
With each deploy that
Upe
is a different set of 3 numbers. I wonder if this has to do with how the function is bundled, assuming the core provider is trying to wrap the function to handle deployment errors (replying to CFN)? Not sure how to debug this.
@thdxr any ideas why this might be failing?
t
This happens before or after the stack starts deploying?
does
sst build
succeed?
d
the moment the CR executes, well into the deploy
build DOES succeed
t
Got it - what us
Upe
?
d
beats me,….. my guess was that after the function is bundled…. the
cr.Provider
is trying to “wrap” the function that is minified and thus trying to run the minified function name?
just a guess
weird… looks like they are creating an additional function that calls our function?
t
Are you sure
Upe is not a function
isn't coming from your lambda code? Seems like a js error
d
Upe
isn’t a function of mine and those three letters change each time I make a change and rebuild
t
are you able to see your function being invoked? if you add log statements for example
d
yes… I was seeing the same error in the logs for the function
t
Does it make it to your code? Or fail before then
d
hmmmm oh… I’ve not added a console but the function is generating that error in the logs
t
Is it possible it's osmething with the nanoid since it's random every time?
d
hmm…. maybe? but doesn’t seem like it. there are not random functions… just strings
f
Hey guys, sorry for the late chime in.
Script
was originally created primarily for the purpose of seeding DB and running DB cleanup scripts, etc, hence the name.
@Dan Van Brunt are you getting this error
Received response status [FAILED] from custom resource. Message returned: Upe is not a function
on deploy and CFN reports this error?
d
@Frank yes this is what CFN reports….. but I can also see
Upe is not a function
in the function logs
doing another test right now with
--no-rollback
does that flag work with sst cli?
t
No it doesn't, I've been thinking about that recently we need to support it. I made an issue a while back
f
@Dan Van Brunt as a side note, you need to set
enableLiveDev
to false in ur Function when used in custom resource.
Copy code
const onEventFunction = new Function(this, 'resource-lambda', {
      handler: 'src/lambda.onEvent',
      memorySize: 512,
      timeout: cdk.Duration.minutes(5),
      enableLiveDev: false,
    })
Btw, i might’ve missed in the conversation above, I didn’t see
Upe
in your code 🤔
t
It's a random string every time he runs it haha
If you haven't already I would add logging statements inside the function so we can see if it's an issue before your code is invoked
f
as another side note, both disabling
enableLiveDev
and assigning a random string (so
onUpdate
gets called on every deploy) are the things the
Script
construct does out of the box 🤓
d
@Frank I don’t think we want any of those features in this ReplicaBucket CR though, do we?
@thdxr How might I go about testing with logging without a --no-rollback option in sst CLI?
t
Post a message to slack 😂
I'm not too sure
Could store something in a database or send an email or something
d
…..hmmmm
t
Hacky I know, I'll be back home in an hour or so and should be quick to add that option
d
is there a hack where I can
sst build
and then
cdk deploy --no-rollback
?
t
potentially but I jsut got home and I'm about to work on it
d
No worries, just didn’t know if that was something that would not require any work. I have more details on the issue…. this is the compiled src for my function…
There is a section where there is a huge amount of text that says…
Please check if you have installed “@aws-sdk/signature-v4-crt” package explicitly.
For more information please go to https://github.com/aws/aws-sdk-js-v3#known-issues``The AWS SDK for JavaScript (v3) will
no longer support Node.js ${e} as of January 1, 2022.
To continue receiving updates to AWS services, bug fixes, and security
updates please upgrade to Node.js 12.x or later.
More information can be found at: https://a.co/1l6FLnu`,“NodeDeprecationWarning
Here are the full logs for my CR function….
@thdxr ok I put a console.log right inside the function handler first thing and it does NOT get fired. After changing that and re-building the error I get now is…
Received response status [FAILED] from custom resource. Message returned: Ipe is not a function
and look what I found in the built code…
t
ah it's a random string because of minification
ok one second
t
I missed that sorry
d
lol nw. I’m only kidding I very much appreciate your help
ok… I can add SOMETHING though… the function that is failing is my logger code
var Ipe=Ss(BO()),qO=Ipe("logger:logs");
its the instantiation for my logger ``logger:logs``
that src looks like this…
Copy code
import * as debug from 'debug'
const logger = debug('logger:logs')
// eslint-disable-next-line no-undef
logger.log = console.log.bind(console)

export default logger
but no idea why that would be an issue
gah! wait.. I think this is a missing node module issue
debug
isnt in my package.json
but its not having any issues during lint or build!?
maybe this?
d
could be… but I added to yarn and still getting the issue. Also its not causing any linting or IDE errors.
dinner break 🥘
t
well I got the rollback feature done
not sure if still relevant though
let me also add a way to disable minifying
d
thanks! its relavent… cause right now I’m racing in the console to get the logs in browser memory before the roll back … so … 😛
t
I published 0.46.0-next.16
It has a
bundle.minify: false
option on the function
and
sst start
will do
--rollback=false
by default and you can do
sst deploy --rollback=false
d
@thdxr how do next versions work in yarn? I litterally just say
yarn add blah@0.46.0-next.16
t
Yep that's right
d
@thdxr Not sure I have it working…. I’m def on
0.46.0-next.16
for both CLI and resources but still getting rollbacks after
yarn deploy --rollback=false
t
are you seeing the minify option in bundle.minify ? (Just to verify it's updated)
Ah you know what I initially had it implemented as
--no-rollback
I might not have remembered to flip the flag back
hm no I definitely pass it through right
d
is there a reason not to just pass any extras thru and to keep the syntax the same? eg
--no-rollback
?
t
The syntax in cdk is actually
--rollback=true/false
and they support
--no-rollback
as an extra option. Reason we're going with
--rollback
is because we want to default it to false on
sst start
and people will need a way to turn that on
Let me try testing this more locally, have to figure out a way to trigger a rollback
d
kk
best way is to use the CR provider and then just make the CR throw
I think that should roll back
t
ok it's definitely working for me
d
I’m still getting
UPDATE_ROLLBACK_COMPLETE
so that means its not working for me right?
do I need to blow away my .build or .sst folders?
t
I don't think so, it looks like that canary release doesn't have the latest
let me try to publish it again
can you actually try
sst deploy --help
and see if the rollback option shows up there
d
yup it does
t
then setting
--rollback=false
should work
although I only tested with sst start, let me try again with sst deploy
Yeah it works for me with deploy. It's possible since the function itself is what's causing the issue, aws will roll it back even with rollback=false
d
hmmm… I think I narrowed down my particular issue. I’ll test again tomorrow this flag