How to bulk delete lambda functions using AWS Cli - amazon-web-services

I have around 200 lambda functions that I need to delete. Using the console I can only delete one at a time, which would be really painful. Does anyone know a cli command to bulk delete all the functions? Thanks!

I've just found an answer using an old script to delete IAM users I had:
aws lambda list-functions --region us-east-1 | jq -r '.Functions | .[] | .FunctionName' |
while read uname1; do
echo "Deleting $uname1";
aws lambda delete-function --region us-east-1 --function-name $uname1;
done

Related

How to delete a bucket starting with a name using aws cli?

Is there any command of aws cli to delete all buckets starting with a specific name?
When I ran:
aws s3 ls
I got so many buckets, we need to frequently cleanup. If got any command, I can create a pipeline for it in gitlab and use that to cleanup.
Like you observe, I want to delete all the buckets starting with somename-
I tried using
aws s3 rb --force s3://somename-*
It didn't work.
There's no built in way to accomplish this. On Unix-like platforms, you can list all of the buckets and use some tools to filter the list and call in the CLI to remove all of the buckets that match some pattern:
aws s3api list-buckets --query 'Buckets[].[Name]' --output text | grep "^somename-" | xargs -n1 -IB echo aws s3 rb s3://B
Remove the "echo " after verifying the command will remove the buckets you want removed.

Is there a way to list CloudFront distributions by tag without downloading them all using ListDistributions?

This question is similar, but the only answer there is to get the list of all distributions and filter them locally, which is not what I am looking for.
This page hints that it might be possible to filter by tag ("You can search and filter your resources based on the tags you add"), but I just cannot figure out how!
To add to Balu's great answer, if you have specified a default region when setting up aws-cli and this region is not us-east-1 then it won't work (you'll see an empty array of ResourceTagMappingList).
Be sure to add --region us-east-1 at the end of the command like so:
aws resourcegroupstaggingapi get-resources --tag-filters Key=tagName,Values=tagValue --resource-type-filters 'cloudfront' --tags-per-page 100 --region us-east-1
I can only think of one way, since aws cloudfront doesn't directly support get distributions by tag.
we can use resourcegroupstaggingapi cli , to get ResourceARNs of all cloudfront and using jq and sed extract DistributionIds
Ex: Tag ApplicationID=APP1111
aws resourcegroupstaggingapi get-resources --tag-filters Key=ApplicationID,Values=APP1111 --resource-type-filters 'cloudfront' --tags-per-page 100 | jq -r ".ResourceTagMappingList[].ResourceARN" | sed 's:.*/::'
we can further use xargs and execute get-distribution for each Id.
aws resourcegroupstaggingapi get-resources --tag-filters Key=ApplicationID,Values=APP1111 --resource-type-filters 'cloudfront' --tags-per-page 100 | jq -r ".ResourceTagMappingList[].ResourceARN" | sed 's:.*/::' | xargs -I {} aws cloudfront get-distribution --id {}

Access updated lambda version from command: `aws lambda publish-version`

My CI pipeline will do two things
generate new lambda version and publish
Update an alias to point at that new version
This will be done via cli commands. My question is, how do I access the version number that been generated from the first command. It is returned and posted to the CLI. Can this be access easily via some nifty was command or will I have to parse it myself?
e.g.
version=$(aws lambda publish-version \
--function-name test_lambda --description "updated via cli" --region eu-west-1 \
--query Version \
--output text)
See Controlling Command Output from the AWS Command Line Interface page of AWS CLI User Guide, specifically How to Filter the Output with the --query Option and Text Output Format
This works but still curious if there is a better way.
version=$(aws lambda publish-version --function-name test_lambda --description "updated via cli" --region eu-west-1| jq '.Version')
NEW_LAMBDA_VERSION=$(aws lambda list-versions-by-function --function-name $LAMBDA_NAME_FOR_DEPLOY --no-paginate --query "max_by(Versions, &to_number(to_number(Version) || '0'))")
NEW_LAMBDA_VERSION=$(echo $NEW_LAMBDA_VERSION | jq -r .Version)
echo $NEW_LAMBDA_VERSION
In this case, I use on .gitlab-ci.yml.

aws lambda list-functions filter out just function names?

I just want to get back a list of function names. Ideally I want to get all functions (just their name) starting with "some-prefix*". Can I do this with the cli?
Really want this as a cli command if possible (I want to avoid python or another sdk). I see there is a --cli-input-json arg, can I use that for filtering?
You can do that. Use the --query option. The CLI would look like this:
aws lambda list-functions --region us-east-1 --query 'Functions[].FunctionName' --output text
To get the list of functions whose name begin with some-prefix:
aws lambda list-functions --region us-east-1 --query 'Functions[?starts_with(FunctionName, `some-prefix`) == `true`].FunctionName' --output text
To get the complete JSON, the CLI would be:
aws lambda list-functions --region us-east-1
Details about the query parameter can be found here.
As the answer is already given by #krishna, but I was looking for a way to print all function name without specifying a prefix. So here you can get all lambda function name in particular region my default is us-west-2.
aws lambda list-functions --query 'Functions[*].[FunctionName]'
Or as I want them out in text format and space separated to use in my bash script so here you can get in text and single line space separated.
aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text | tr '\r\n' ' '
I have come here for some help to clean up all lambda functions that I have created while following an AWS developer certification tutorial. If anyone is in the same boat, I have created a script to programmatically delete all lambda functions in my AWS account (NOT for production use)
#!/bin/bash
# STOP: DON'T USE/RUN THIS SCRIPT UNLESS YOU ARE 100% SURE WHAT YOU ARE DOING
# I am a learner and created 20+ lambda functions while following a tutorial
# I wrote this script to programatically cleanup functions at the end of course
# precondition: your aws cli is configured
# get all functions from aws account
functions=(`aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text`)
for i in "${functions[#]}"
do
#delete functions 1-by-1
aws lambda delete-function --function-name "$i"
echo "deleted $i"
done
Incase, someone is looking for a similar query with string present in the lambda function name as a substring, try below
aws lambda list-functions --region us-east-1 --query 'FunctionName[?contains(FunctionName, 'containing-string'] == 'true'].[FunctionName]' --output text
Note - the '[]' brackets around '.FunctionName' will provide with each fucntionName on a new line.
You can easily get the list of all lambda functions in given region using below command:
aws lambda list-functions --region us-east-1 | jq -r .Functions[].FunctionName
Download the jq (Lightweight and flexible command-line JSON processor) from here:
https://stedolan.github.io/jq/download/

How do you delete an AWS ECS Task Definition?

Once you've created a task definition in Amazon's EC2 Container Service, how do you delete or remove it?
It's a known issue. Once you de-register a Task Definition it goes into INACTIVE state and clutters up the ECS Console.
If you want to vote for it to be fixed, there is an issue on Github. Simply give it a thumbs up, and it will raise the priority of the request.
I've recently found this gist (thanks a lot to the creator for sharing!) which will deregister all task definitions for your specific region - maybe you can adapt it to skip some which you want to keep: https://gist.github.com/jen20/e1c25426cc0a4a9b53cbb3560a3f02d1
You need to have jq to run it:
brew install jq
I "hard-coded" my region, for me it's eu-central-1, so be sure to adapt it for your use-case:
#!/usr/bin/env bash
get_task_definition_arns() {
aws ecs list-task-definitions --region eu-central-1 \
| jq -M -r '.taskDefinitionArns | .[]'
}
delete_task_definition() {
local arn=$1
aws ecs deregister-task-definition \
--region eu-central-1 \
--task-definition "${arn}" > /dev/null
}
for arn in $(get_task_definition_arns)
do
echo "Deregistering ${arn}..."
delete_task_definition "${arn}"
done
Then when I run it, it starts removing them:
Deregistering arn:aws:ecs:REGION:YOUR_ACCOUNT_ID:task-definition/NAME:REVISION...
Oneline approach inspired by Anna A reply:
aws ecs list-task-definitions --region eu-central-1 \
| jq -M -r '.taskDefinitionArns | .[]' \
| xargs -I {} aws ecs deregister-task-definition \
--region eu-central-1 \
--task-definition {} \
| jq -r '.taskDefinition.taskDefinitionArn'
There is no option to delete a task definition on the AWS console.
But, you can deregister (delete) a task definition by executing the following command number of revisions that you have:
aws ecs deregister-task-definition --task-definitiontask_defination_name:revision_no
Created following gist to safely review, filter and deregister AWS task-definitions and revisions in bulk (max 100 at a time) using JS CLI.
https://gist.github.com/shivam-nagar/aa79b02b74f616f8714d51e419bd10de
Can use this to deregister all revisions for task-definition. This will result in task-definition itself marked as inactive.
Now its supported
I just went inside the Task Definations and clicked on Actions and click on Deregister and it was removed from the UI