4

Because lambdas are stateless and multiple instances can run at the same time it might be a bad idea to generate ids based on timestamps. I am currently using UUIDv1. I know the chance of generating the same IDs with the same timestamp already literally impossible. It's also enough unique enough for my application. Out of curiosity I'm thinking of ways to generate truly mathematically unique ids on aws lambda.

UUID v1 uses a node to distinct ids generated with the same timestamp. Random numbers or MAC-Adresses (bad idea for virtual instances) are used to create node ids.

If I had a unique id for my active lambda instance I would be able to generate truly unique ids. There is a awsRequestId inside the context object but it just seems like another timestamp based UUID.

Maybe you guys have more ideas?

Mike
  • 20,010
  • 25
  • 97
  • 140
Jingyi Wang
  • 834
  • 7
  • 17
  • Does this answer your question? [How can I uniquely ID the AWS Lambda container that my function is running in?](https://stackoverflow.com/questions/43213732/how-can-i-uniquely-id-the-aws-lambda-container-that-my-function-is-running-in) – Dunedan Apr 10 '20 at 19:31
  • The awsRequestId appears to be a UUID V4 which should be unique for all practical purposes across any number of instances. Why is it not sufficient? – stdunbar Apr 12 '20 at 03:15

2 Answers2

1

AWS lambda:

System.getenv("AWS_LAMBDA_LOG_STREAM_NAME").replaceFirst(".*?(?=\\w+$)", EMPTY)

Defined runtime environment variables

AWS EC2:

httpGet http://169.254.169.254/latest/meta-data/instance-id

Instance metadata and user data

AWS ECS:

httpGet http://localhost:51678/v1/metadata

How to get Task ID from within ECS container?

Mike
  • 20,010
  • 25
  • 97
  • 140
0

Unique within subnet

String executableId = ManagementFactory.getRuntimeMXBean().getName();
Mike
  • 20,010
  • 25
  • 97
  • 140