Failed to create functions during sam deploy - amazon-web-services

I am creating a project in cloud9 with AWS Lambda and Amazon EFS resources so that users can use a machine learning model.
I was able to build the project successfully, but no matter what I do, sam deploy --guided fail and error message is:
Error: Failed to create/update the stack: sam4-app, Waiter StackCreateComplete failed: Waiter encountered a terminal failure state: For expression "Stacks[].StackStatus" we matched expected path: "ROLLBACK_COMPLETE" at least once.
In the aws cloudFormation console, I found that the creation of my two functions failed
I am sure I am doing something wrong, yet after so much struggle I have not been able to identify my mistake.
Do I need to make any changes inside the template.yml in order to use AWS Lambda?
It is important to mention that I am taking as reference multiple machine learning models for inference on AWS Lambda and Amazon EFS
Also I have dumb question, I am using windows inside cloud9, could this be causing this error?

You are trying to use too much memory. You are trying to provision 5000MB for your function. While a limit of 10GB is now possible in some regions, it isn't supported in all regions. In the regions where 10GB is not supported, the old 3008MB limit applies.

Related

How to fix "The Following Resources Failed to Update" in Cloudformation?

I have a multi-account pipeline using AWS CodePipeline that is failing in the prod account. The pipeline will work in dev and test but fails in prod and the Cloudformation stack gives the error: "The Following Resources Failed to Update:" and lists several Lambda functions. Anyone know how to fix this? I've checked the permissions and compared them to the other accounts and they seem to match. From my understanding the resources are under the control of CF and should be able to be changed via CDK?
The CDK definitions of the Lambda functions seem to match their current configurations in AWS, So I am having trouble understanding why it no longer works.
The resources also were "drifted" and to fix that I deleted the resource from the stack and made sure to include the "DeletionPolicy: "Retain" before deleting. I of course Imported those resources but I am wondering if that could be the source of the issue?
Really racking my brain with this one

Is it possible to create AWS CloudFormation template, that will create a stack with the CREATE_FAILED status

For testing reasons I need to deploy a failed stack via CloudFormation (with CREATE_FAILED status), but when I try to mess with the CloudFormation template, it stops me on the template validator. I believe that it is possible to create a template, that will pass the validator but fail when deployed. Can someone give me an example? (Free tier examples will be the best ones)
Yes, if resource was trying to create and it failed. It may fail if it was there already, it may fail because of API error. It may fail if e.g. you create lambda with concurrency 100000 and you don't have that available on your account. You should see the reason inside cloudformation console OR if you used cli, just don't terminate the process in cli and it will show the reasons. Yet sometimes, especially when using sub-stacks i find more useful info in aws console itself.

How can you use AWS Lambda scripts to deploy AWS Infrastructure with Terraform

I have already my whole AWS infrastructure set up in Terraform and everything works fine. So now, instead of deploying it from my local machine running terraform apply, I want to deploy my Infrastructure with an AWS Lambda Script completely serverless. Is there anyone who knows how to do this or where to read about this concept? Didn't find anything useful on the internet until now.
I think my sourcecode could lie on a S3 Bucket and the Lambda function grabs it, and runs it in terraform also set up in the function itself i guess due to terraform is such a small program.
I would attempt that as follows:
Create a lambda container image which would include official terraform binary. The actual lambda function code would use, lets say, python's python-terraform package to interact with the binary. Or directly invoke the binary using subprocess.run.
Setup a lambda execution role with all the permissions needed for creation of your resources.
Create a lambda function using the container image.
I haven't tried that personally yet, but I think it is something that should work.

How to update AWS Fargate service outside AWS code deploy in order to change desired task count

When set up AWS code deploy to deploy an AWS service we have to provide 2 target groups lets say
TargetGroupBlue and TargetGroupGreen.
In the cloudformation template we use the TargetGroupBlue when linking the Service to Loadbalancer.
TargetGroupGreen is created only to be used by AWS during code deploy.
Step 1 : We executed create stack command in order to create the service and loadbalancer. We have a workable service now. Traffic is routed via TargetGroupBlue.
Step 2 : Then use code deploy to do another deploy which will the swap the target group to TargetGroupGreen once done.
Step 3 : Now we need to update the desired task count in service so use cloudformation update stack command. This fails because the targetgroup is TargetGroupGreen (as Code deploy changed it in step 2) and out cloud formation templates has used TargetGroupBlue for linking the service to Loadbalancer.
The workaround could be do all service related updates outside code deploy in a even numbered release (so always have to do code deploy twice so that we know traffic is always routed TargetGroupBlue)
Is this the way we should work with service updates via cloudformation and Code Deploy?
Please help to get this figured out.
Even though AWS provides many cool ways to work with when it comes to BlueGreen deploys with CodeDeploy or CloudFormation it really sucks.
The work around they suggested was to use Custom Resources in cloudformation which will actually trigger a lambda function to get the services updated cheating the cloudformation stack updates. Sample.
But there are no proper samples to do that so it would take lot of time to get it to work the way you need.
Furthermore, the cloudforamtion with hooks does not really work for bigger projects as the LBs cannot be shared.
So here is the open ticket, please help to put a thumbs up so the AWS will prioritize this in their roadmap.
https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/483

How do I run a AWS Lambda function to let me know that CloudFormation has completed the entire stack creation

I would like to make an automated call to a custom program API as soon as CloudFormation has completed the entire stack creation (deployment of instances, setup of VPC, Puppet scripts, etc.).
What is the correct way to go about this?
After some research, it seems a good option would be to launch an AWS Lambda function triggered by the event that stack creation has been completed successfully, but I have no idea how to approach this.
Any ideas or advice would be appreciated.
You can provision and coordinate a lot of what you're talking about (setup of VPC, etc.) with CloudFormation, the DependsOn attribute and nested CloudFormation stacks. This way you can order the execution of the CloudFormation stacks so that, for example, your VPC is created first followed by launching your EC2 instance(s) followed by the deployment of the software on the instance(s).
You can also coordinate the execution of the other behavior you mentioned (deployment [on] instances, [calling] Puppet scripts, etc.) using AWS::CloudFormation::Init. This way, you can call out to your Puppet scripts from your EC2 instance within the CloudFormation template. The actual execution of your Puppet scripts occurs on the EC2 instance(s).
If you want to see an example of calling out to a configuration management tool from CloudFormation (in this case, we're using Chef Solo), see app-instance.json.
If you'd like to see an example of using nested stacks, see dromedary-master.json.
There's also some examples of using Lambda on our blog as well (Stelligent), but it doesn't seem like you need to use Lambda in this case based on the problem you're trying to solve.
P.S. You don't have to use nested stacks either, but it can make things a little cleaner. But, you do want to control the creation order of the resources so the DependsOn attribute will help you in doing so.