[英]boto3 : SSM Parameter get_parameters()
I am creating lambda function where I am fetching SSM parameter for EKS-Optimized AMI ID, now about EKS-Optimized AMI, it is the default AMI provided by EKS if we are not specifying any AMI explicitly.我正在创建 lambda function,其中我正在为 EKS 优化的 AMI ID 获取 SSM 参数,现在关于 EKS 优化的 AMI,如果我们没有明确指定任何 AMI,它就是 EKS 提供的默认 AMI。 EKS-Optimized AMI are different per region & K8 version.
EKS 优化的 AMI 因区域和 K8 版本而异。 I am working on upgrading this AMI on node groups & getting this AMI ID here for K8 version 1.21.
我正在努力在节点组上升级此 AMI,并在此处为 K8 版本 1.21 获取此 AMI ID。 I want to pass this k8 version ${EKS_VERSION} to get-parameter() as
我想将这个 k8 版本 ${EKS_VERSION} 传递给 get-parameter() 作为
ami_id = ssm.get_parameters(Names=["/aws/service/eks/optimized-ami/${EKS_VERSION}/amazon-linux-2/recommended/image_id"])
Can you help me if we can do this in boto3, if yes,how?如果我们可以在 boto3 中做到这一点,你能帮我吗?如果可以,怎么做? Thanks in advance!
提前致谢!
Maybe I am missing the point of the question but it is pretty straightforward as you already have the request in your question.也许我错过了问题的重点,但它非常简单,因为您已经在问题中提出了请求。 If you put the following code into your lambda, it should get you the version you want in that region.
如果您将以下代码放入您的 lambda,它应该会为您提供该地区所需的版本。
For something like this, you may want to use a lambda env variable with a default, and overwrite it when you want something different.对于这样的事情,您可能希望使用默认的 lambda env 变量,并在您想要不同的东西时覆盖它。
import boto3
import ssm
# get an ssm client
ssm_client = boto3.client('ssm')
# you need to pass the var somehow, here assuming you are using an environment variable in your lambda. You could use some other system to trigger and pass the information to your lambda, e.g. sns
eks_version = os.getenv('EKS_VERSION')
# set the parameter name you want to receive, note the f-string to pass the variable to it
param_name = f"/aws/service/eks/optimized-ami/{eks_version}/amazon-linux-2/recommended/image_id"
# get_parameter
response = ssm_client.get_parameters(Names=[param_name])
# print / return response
print(response)
For overwriting the param, you could use sns or cloudwatch with lambda if you are building some kind of automation but you would need to parse the input from them.为了覆盖参数,如果您正在构建某种自动化,则可以将 sns 或 cloudwatch 与 lambda 结合使用,但您需要解析来自它们的输入。 For example a simple json payload in sns
例如 sns 中的一个简单的 json 有效载荷
{
"eks_version": 1.21
}
and in your code, you can change make small adjustment once you parsed the sns payload.在您的代码中,您可以在解析 sns 有效负载后进行小幅调整。 eg
例如
import json
if 'Sns' in the event:
sns_eks_version = json.loads(event['Records'][0]['Sns']['Message']['eks_version'])
else:
sns_eks_version = None
eks_version = sns_eks_version or os.get_env('EKS_VERSION')
This is how I did:我就是这样做的:
import json
import os
import boto3
ssm_client = boto3.client('ssm')
eks_client = boto3.client('eks')
eksClusterName='dev-infra2-eks'
def lambda_handler(event, context):
# Get current EKS Version
response = eks_client.describe_cluster(
name = eksClusterName
)
eksVersion = response['cluster']['version']
aws_eks_ami_ssm_param = "/aws/service/eks/optimized-ami/"+eksVersion+"/amazon-linux-2/recommended/image_id"
# Get SSM param for AMI ID
try:
eks_ssm_ami = ssm_client.get_parameter(Name=aws_eks_ami_ssm_param)
latest_ami_id = eks_ssm_ami['Parameter']['Value']
return latest_ami_id
except client.exceptions.ParameterNotFound:
logging.error("Parameter Not Found")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.