AWS SAM nested stacks - aws-lambda

I'm using nested stack with SAM, and I have a parent stack which contains SNS resource and two children, childStack1 want to publish a message to the SNS topic, childStack2 wants to get notified from the same SNS, but how can I access the shared SNS topic from childStack1/2 yaml or lambda?
ParentStack:
├── ChildStack1
|-------NestedFunction1
│-------template.yaml
├── ChildStack2
|-------NestedFunction2
|-------template.yaml
template.yaml <--this contains SNS resource
Thx...

Related

AWS cloudformation custom resource to generate config file for another lambda

I want to generate a lambda's config file dynamically (Basically application config) during the AWS stack creation.
Once all the configs are ready then only the particular lambda should be created along with that newly generated file. Can I achieve this using custom resources in AWS cloud formation?
I searched but only with lambda or commandrunner or SNS topics only there. No custom resource to write or modify local files. Could someone provide a sample or guidance to do this ?
Here's some options I see for your use case:
Use a Lambda based CF Custom Resource for your config file logic. Load base files from S3 or checkout from Version Control (git) within the Custom Resource Lambda function.
Execute a custom script within your build/deploy process. E.g. you have a build.sh script that contains the commands to deploy the CF templates, but first you execute another script that creates the config file and places it in the source folder for the lambda function.
Use a Docker Image based Lambda function and include your config file logic in the Dockerfile. You can also use AWS SAM to build the docker image within the CF deployment.
Use AWS CDK and its concept of bundling for lambda functions.

Get ARN of vendored layers

Looks like AWS layers like AWSLambda-Python37-SciPy1x have a different account and head version in the ARN in different regions. Eg
us-east-1: arn:aws:lambda:us-east-1:668099181075:layer:AWSLambda-Python37-SciPy1x:22
us-east-2: arn:aws:lambda:us-east-2:259788987135:layer:AWSLambda-Python37-SciPy1x:20
From a script I need to add the layer that pertains to the lambda's region, but I'm not finding an AWS CLI or boto3 command that will give me the ARN of a "published" layer (ie one that was given access to by an AWS admin to all accounts), I can only find my own layers (eg aws lambda list-layers).
The AWS console for lambda in web browser shows the vendored layers, so I loaded the page and looked through js console and saw the following request is made:
https://console.aws.amazon.com/lambda/services/ajax?operation=listAwsVendedLayers&locale=en
So it looks like the REST API has this operation to get that, but I cannot find the equivalent anywhere in AWS CLI or boto3.
Any ideas (short of using curl with the proper request head and auth info, pain), perhaps a way to run a "raw" request in boto3 so I could give it this listAwsVendedLayers operation? I looked in the docs could not find anything.

How to apply Serverless to an existing Lambda

When I try deploy to an existing lambda function configured in serverless.yml as following, it says "An error occurred: ApiLambdaFunction - an-existing-function-name-created-by-my-devops already exists."
functions:
api:
name: an-existing-function-name-created-by-my-devops
So it is not allowed to deploy to an existing lambda not created by serverless?
As Serverless manages your resources via a CloudFormation Stack, you could probably be able to import the lambda function within the UI (Import Existing Resources into a CloudFormation Stack) and do the deploy afterwards again.
I did not try this and there's most probably a better solution though.
Edit: precondition is that you successfully created your stack before adding your desired function.

Check CW log-group of a lambda through CLI

I have only programmatic access to AWS cloud watch and other services. I created a lambda and event source mapping with DynamoDB. It uploaded and ran successfully but I don't know which log group is it logging. Is there a way through the CLI to check which log group a lambda is configured with?

Updating a CloudFormation stack if codebase updates

So I have an existing CloudFormation stack up and running. However, I haven't found a solution for my problem, which is that I want my resources, for example EC2 and Lambda, to have up to date code.
It seems that a CloudFormation stack doesn't update if the template doesn't have any changes. I'm holding my code inside a S3 bucket as a zip-file, but if this file gets changed, CloudFormation doesn't notice it.
Is my best bet creating a git hook script that uses AWS CLI and updates the EC2 and Lambda code or is there some 'elegant' way for CloudFormation to notice these changes?
Create a new lambda function to update your existing lambda and ec2 or call the cloud formation to update them. On your S3, create an object Put event and call that new lambda function. So whenever a new file(zip) is put in s3, your ec2 & lambda gets updated.

Resources