简体   繁体   English

boto3: SSM 参数 get_parameters()

[英]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.

相关问题 Python Boto3 使用 NextToken 按路径从 SSM 获取参数 - Python Boto3 Get Parameters from SSM by Path using NextToken Boto3 get_records:输入中缺少必需参数:“ShardId” - Boto3 get_records: Missing required parameter in input: "ShardId" Boto3:动态获取凭据? - Boto3: get credentials dynamically? 使用 boto3 创建 SSM 文档以关闭微服务及其关联的 EC2 实例 - create SSM document with boto3 that shuts down microservices and their associated EC2 instances boto3 如何通过 python 脚本使用 AWS SSM 服务安装 cloud watch agent - boto3 How to install cloud watch agent using AWS SSM service through python script 如何使用 python boto3 检查具有特定标记值的所有服务器的 ssm 连接状态 - How to check the ssm connection status of all the servers with a specific tag value using python boto3 从子文件夹 Boto3 中获取所有文件 - Get all File from Subfolder Boto3 Ansible aws_secret 模块返回“aws_ssm 查找需要 botocore 和 boto3”,尽管它们已与 pip 一起安装 - Ansible aws_secret module returns "botocore and boto3 are required for aws_ssm lookup" despite both having been installed with pip 使用 boto3 获取 dynamodb 中的分区键列表 - get the list of partition keys in a dynamodb with boto3 AWS CLI 或 boto3:尝试获取可用性区域 ID? - AWS CLI or boto3: Trying to get the availability-zone id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM