简体   繁体   English

使用 boto3 创建帐户后,如何通过电子邮件自动邀请 IAM 用户?

[英]How to automatically invite an IAM user via email after creating account using boto3?

I'm creating an IAM user programmatically using boto3 .我正在使用boto3编程方式创建 IAM 用户。

I also want to invite the user that I've just created using email.我还想邀请我刚刚使用电子邮件创建的用户。

Here's what I've tried so far for creating and updating the password.这是我迄今为止尝试创建和更新密码的方法。

iam.create_user(UserName='username')
iam.create_login_profile(
    UserName='username',
    Password='password',
    PasswordResetRequired=False
)

But I haven't found an option to automatically send an invite email to the user after it's been created.但是我还没有找到在创建后自动向用户发送邀请电子邮件的选项。

Is there any way to automatically send an invite mail with the password and so that user can login?有没有办法自动发送带有密码的邀请邮件,以便用户可以登录?

Something like就像是

invite_mail='somemail'

There is no in-built AWS capability to send users their login information.没有内置的 AWS 功能可以向用户发送他们的登录信息。

In fact, there is not even a standard field for storing email addresses for IAM Users.事实上,甚至没有用于存储 IAM 用户电子邮件地址的标准字段。

You would need to code such functionality yourself.您需要自己编写此类功能。

1 1

You can use a cloud trail to trigger Lambda upon the IAM-user creation event and send email to newly created users using AWS SES client.您可以使用云跟踪在 IAM 用户创建事件时触发 Lambda,并使用 AWS SES 客户端向新创建的用户发送电子邮件。 You can validate the format of the email with a regex like abc.xyx@company.com.您可以使用像 abc.xyx@company.com 这样的正则表达式来验证电子邮件的格式。 This you can only do if the user name is in email format.仅当用户名采用电子邮件格式时才能执行此操作。

2 2

import boto3

import logging

ses_client = boto3.client('ses', region_name='us-east-1')

iam_client = boto3.client('iam')


response = iam_client.create_user(
    Path='string',
    UserName='string',
    PermissionsBoundary='string',
    Tags=[
        {
            'Key': 'string',
            'Value': 'string'
        },
    ]
)

#if username as email id 
user_email =  response['User']['UserName']

#if user has tagged with email

#user_email =  response['User']['Tags']['KeyName']

SMTP_FROM = 'EMAIL_ADDRESS'

html = "html_email_template"

SMTP_TO = user_email

try:
    response = ses_client.send_email(
        Source=SMTP_FROM,
        Destination={
            'ToAddresses': SMTP_TO 
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': "UTF-8",
                    'Data': html,
                }
            },
            'Subject': {
                'Data': 'New User Created '
            }
        }
    )
    logger.info(response)
except ClientError as e:
    logger.error(e.response['Error']['Message'])
else:
    logger.info("Email sent! Message ID: " + response['MessageId'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM