<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: StintriLamah</title>
    <description>The latest articles on Forem by StintriLamah (@stintrilamah).</description>
    <link>https://forem.com/stintrilamah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1253069%2Ff353693d-265e-4b00-8e48-5252cbcc7be1.jpg</url>
      <title>Forem: StintriLamah</title>
      <link>https://forem.com/stintrilamah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/stintrilamah"/>
    <language>en</language>
    <item>
      <title>Leveraging IRSA with AWS SES To Send Emails from EKS Pods</title>
      <dc:creator>StintriLamah</dc:creator>
      <pubDate>Sun, 25 Aug 2024 08:16:27 +0000</pubDate>
      <link>https://forem.com/stintrilamah/leveraging-irsa-with-aws-ses-to-send-emails-from-eks-pods-c8c</link>
      <guid>https://forem.com/stintrilamah/leveraging-irsa-with-aws-ses-to-send-emails-from-eks-pods-c8c</guid>
      <description>&lt;p&gt;By the end of the article, readers will have a clear understanding of how to configure their EKS (Elastic Kubernetes Service) clusters to send emails using Amazon SES (Simple Email Service) in a different AWS account, leveraging the power of IRSA (IAM Roles for Service Accounts) to manage permissions securely and efficiently. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;IRSA and its Benefits&lt;/li&gt;
&lt;li&gt;IRSA Configuration&lt;/li&gt;
&lt;li&gt;Cross-Account Access&lt;/li&gt;
&lt;li&gt;SES Configuration&lt;/li&gt;
&lt;li&gt;Testing and Validation&lt;/li&gt;
&lt;li&gt;Best Practices and Security Considerations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Managing email communications from Kubernetes pods within Amazon EKS  can be challenging, especially when Amazon SES is located in a different AWS account. Traditionally, managing IAM credentials and securely configuring permissions across accounts involves complex setups and potential security risks of using Access Keys that can be compromised. &lt;/p&gt;

&lt;p&gt;The problem intensifies when developers need to ensure that their applications can send emails efficiently and securely without compromising on the principles of least privilege and access management.&lt;/p&gt;

&lt;p&gt;This guide will help developers and DevOps engineers simplify their setup, enhance security, and streamline their email-sending workflows from Kubernetes pods. This is not only limited to SES but other AWS services that permissions can be managed by roles.&lt;/p&gt;

&lt;h3&gt;
  
  
  IRSA and its Benefits
&lt;/h3&gt;

&lt;h4&gt;
  
  
  What is IAM Roles for Service Accounts (IRSA)
&lt;/h4&gt;

&lt;p&gt;IRSA allows Kubernetes service accounts to assume IAM roles, similar to the way that Amazon EC2 instance profiles provide credentials to Amazon EC2 instances. Instead of creating and distributing your AWS credentials to the containers or using the Amazon EC2 instance's role, you associate an IAM role with a Kubernetes service account and configure your Pods to use the service account. This enables pods running on EKS to interact with AWS services securely without embedding AWS credentials in the pods. Applications in a Pod's containers can use an AWS SDK or the AWS CLI to make API requests to AWS services using AWS Identity and Access Management (IAM) permissions.&lt;/p&gt;

&lt;p&gt;The following steps explains how IRSA works to assign the pod temporary credentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmns8dh9mpjn1szb53z5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmns8dh9mpjn1szb53z5.jpg" alt="IRSA Flow Diagram" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A reference between the EKS Cluster and IAM is established via OIDC. This is a one-time setup per cluster.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A reference between a Kubernetes service account and an IAM Role has to be created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Kubernetes resource is configured with an appropriate service account annotation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As soon a Pod with a service account annotation comes up, the Pod Identity Webhook will be triggered and reconfigure (mutate) the Pod to use IRSA.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Pod assumes the specified IAM Role and connects to the AWS Security Token Service.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AWS STS verifies the request by contacting AWS IAM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the request could be verified and is valid, AWS STS assigns temporary credentials.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Benefits of using IRSA for managing permissions in EKS
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; No need to store long-term AWS credentials in pods by using Access Keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fine-Grained Access:&lt;/strong&gt; Assign least privilege permissions to specific workloads. You can scope IAM permissions to a service account, and only Pods that use that service account have access to those permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Credential isolation:&lt;/strong&gt; A Pod's containers can only retrieve credentials for the IAM role that's associated with the service account that the container uses. A container never has access to credentials that are used by other containers in other Pods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Auditability:&lt;/strong&gt; Access and event logging is available through AWS CloudTrail to help ensure retrospective auditing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  IRSA Configuration
&lt;/h3&gt;

&lt;p&gt;Before configuring IRSA, ensure that the EKS cluster has OIDC enabled.&lt;/p&gt;

&lt;p&gt;The following configurations will be done on the account where the EKS Cluster and pods are running.&lt;/p&gt;

&lt;h4&gt;
  
  
  Create IAM Policy
&lt;/h4&gt;

&lt;p&gt;Create an IAM policy that allows sending emails using SES.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"ses:SendEmail"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"ses:SendRawEmail"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:ses:&amp;lt;REGION&amp;gt;:&amp;lt;SES_ACCOUNT_ID&amp;gt;:identity/&amp;lt;SENDER_EMAIL&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:ses:&amp;lt;REGION&amp;gt;:&amp;lt;SES_ACCOUNT_ID&amp;gt;:identity/&amp;lt;RECEIVER_EMAIL&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we are using the SES Sandbox environment, both the Sender and Receiver needs to be verified on SES. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is different on the Production environment where the receivers will not need any verification hence on the IAM Policy the arn of the receiver won't be needed.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create an IAM Role with Trust Policy
&lt;/h4&gt;

&lt;p&gt;Create an IAM role with a trust policy that allows the EKS service account to assume the role.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Principal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Federated"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:iam::&amp;lt;EKS_ACCOUNT_ID&amp;gt;:oidc-provider/&amp;lt;OIDC_PROVIDER&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sts:AssumeRoleWithWebIdentity"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Condition"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"StringEquals"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"&amp;lt;OIDC_PROVIDER&amp;gt;:sub"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"system:serviceaccount:&amp;lt;NAMESPACE&amp;gt;:&amp;lt;SERVICE_ACCOUNT_NAME&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"&amp;lt;OIDC_PROVIDER&amp;gt;:aud"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"sts.amazonaws.com"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Associate the IAM Role with a Kubernetes Service Account
&lt;/h4&gt;

&lt;p&gt;Create a Kubernetes service account and annotate it with the IAM role ARN created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apiVersion: v1
kind: ServiceAccount
metadata:
  name: &amp;lt;SERVICE_ACCOUNT_NAME&amp;gt;
  namespace: &amp;lt;NAMESPACE&amp;gt;
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::&amp;lt;EKS_ACCOUNT_ID&amp;gt;:role/&amp;lt;ROLE_NAME&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Cross-Account Access
&lt;/h3&gt;

&lt;p&gt;Accessing AWS resources across different accounts can be complex due to different security boundaries and policies. The key challenge is to securely grant permissions to SES resources from the EKS cluster in another account.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Account Trust Policies:&lt;/strong&gt; Establish trust policies that allow IAM roles in EKS Account to access SES in the different account.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IRSA Configuration:&lt;/strong&gt; Use IRSA to link EKS service accounts with IAM roles, enabling secure communication.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  SES Configuration
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Verify Your Domain or Email Address.
&lt;/h4&gt;

&lt;p&gt;In SES we have two environments (Sandbox and Production). All new accounts come configured with the SES Sandbox and you need to request it to be converted to the SES Production. (&lt;a href="https://docs.aws.amazon.com/ses/latest/dg/request-production-access.html" rel="noopener noreferrer"&gt;How to Request SES Production Access&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;For the demo I will be using SES SandBox and email address to configure the email identities. I will create two identities one for the sender and the other for the receiver. In SES Sandbox, you need both sender and recipient emails to be verified. This restriction is in place to prevent spam and misuse of the service while you're testing.&lt;/p&gt;

&lt;p&gt;When your account has moved out of the sandbox and into production, you can send email to any recipient, regardless of whether the recipient's address or domain is verified. However, you still have to verify all identities that you use as "From", "Source", "Sender", or "Return-Path" addresses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb2qnwl58sg82tr409g1r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb2qnwl58sg82tr409g1r.png" alt="New SES Identity" width="800" height="626"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create an Authorization policy for Sending Email Identity
&lt;/h4&gt;

&lt;p&gt;Create an authorization Policy for the Sender Email Address identity to allow the Service Account role created to send mails&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzmumataol0d4s0rrdio7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzmumataol0d4s0rrdio7.png" alt="SES Sender Authorization Config" width="800" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcrelspxmagcz0f0r0555.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcrelspxmagcz0f0r0555.png" alt="SES Sender Policy Config" width="800" height="649"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Principal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="nl"&gt;"AWS"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:iam::&amp;lt;EKS_ACCOUNT_ID&amp;gt;:role/&amp;lt;SERVICE_ACCOUNT_NAME_ROLE&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"ses:SendEmail"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"ses:SendRawEmail"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:ses:&amp;lt;REGION&amp;gt;:&amp;lt;SES_ACCOUNT_ID&amp;gt;:identity/&amp;lt;SENDER_EMAIL&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing and Validation
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Deploy a sample application in your EKS cluster that uses the service account to send emails via SES.
&lt;/h4&gt;

&lt;p&gt;Use the following deployment file to create a pod that uses an image with AWS CLI so that you can test sending the mail via CLI.&lt;/p&gt;

&lt;p&gt;This will create a deployment with one replica in the default namespace and use the Service Account created with the IAM role configured&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: ses-test
  labels:
    app: ses-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ses-test
  template:
    metadata:
      labels:
        app: ses-test
    spec:
      serviceAccountName: &amp;lt;SERVICE_ACCOUNT_NAME&amp;gt;
      containers:
      - name: ses-test
        image: amazon/aws-cli
        &lt;span class="nb"&gt;command&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"sh"&lt;/span&gt;, &lt;span class="s2"&gt;"-c"&lt;/span&gt;, &lt;span class="s2"&gt;"sleep 3600"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Confirm the pod is running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl get pods | &lt;span class="nb"&gt;grep &lt;/span&gt;ses-test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Verify that your application can send emails by sending a test email
&lt;/h4&gt;

&lt;p&gt;Once the pod is running, execute the below commands to confirm that you can send an email from the pod.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &amp;lt;POD_NAME&amp;gt; &lt;span class="nt"&gt;--&lt;/span&gt; /bin/sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws ses send-email &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--region&lt;/span&gt; &amp;lt;REGION&amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--from&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;SENDER_EMAIL&amp;gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--source-arn&lt;/span&gt; &lt;span class="s2"&gt;"arn:aws:ses:&amp;lt;REGION&amp;gt;:&amp;lt;SES_ACCOUNT_ID&amp;gt;:identity/&amp;lt;SENDER_EMAIL&amp;gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--destination&lt;/span&gt; &lt;span class="s2"&gt;"ToAddresses=[&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;RECEPIENT_EMAIL&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;]"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--message&lt;/span&gt; &lt;span class="s2"&gt;"Subject={Data=Test email from EKS pod,Charset=utf-8},Body={Text={Data=This is a test email sent from a pod in EKS Account using SES in a different account.,Charset=utf-8}}"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5afz0amopyw9ennh19qi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5afz0amopyw9ennh19qi.png" alt="Validation of Mail Received" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Practices and Security Considerations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Limit Permissions:&lt;/strong&gt;  Assign only the necessary permissions to your IAM roles to follow the principle of least privilege.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor Access:&lt;/strong&gt; Regularly monitor access and review IAM policies and roles for any changes or anomalies.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kubernetes</category>
      <category>security</category>
      <category>aws</category>
      <category>devops</category>
    </item>
    <item>
      <title>AWS Automated Centralized Multi Account Patching</title>
      <dc:creator>StintriLamah</dc:creator>
      <pubDate>Mon, 06 May 2024 16:50:28 +0000</pubDate>
      <link>https://forem.com/stintrilamah/aws-automated-centralized-multi-account-patching-50oe</link>
      <guid>https://forem.com/stintrilamah/aws-automated-centralized-multi-account-patching-50oe</guid>
      <description>&lt;h3&gt;
  
  
  AWS Automated Centralized Multi Account Patching
&lt;/h3&gt;

&lt;p&gt;This is a walkthrough on the steps needed to set up the Systems Manager Management Account and Target accounts for Patching.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup Resource Groups to Logically Group your Managed Instances&lt;/li&gt;
&lt;li&gt;Setup the Required IAM permissions on Management account&lt;/li&gt;
&lt;li&gt;Setup the Required IAM permissions on Target account&lt;/li&gt;
&lt;li&gt;Create an Automation Document to Execute Patch Baselines&lt;/li&gt;
&lt;li&gt;Execute Automation to Patch Multi Account Resources&lt;/li&gt;
&lt;li&gt;Schedule Event Bridge Invocation during Patching Window&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d298395ezg12xdcay7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d298395ezg12xdcay7z.png" alt="Architecture Design" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Many organizations struggle with effectively managing vulnerabilities and patching across their various environments, such as Production, UAT, and Staging. AWS Systems Manager Automation offers a solution by supporting multi-account and multi-Region actions, allowing centralized management of AWS resources. This capability streamlines configuration, operational tasks, and compliance efforts across the enterprise.&lt;/p&gt;

&lt;p&gt;In this article, I'll illustrate how to utilize Resource Groups for organizing instances for patching purposes. For instance, you can create Resource Groups for different environments like development, test, and production. Additionally, I'll demonstrate the creation of a custom Automation Document that harnesses Patch Manager. Finally, I'll guide you through executing this custom Automation Document to install patches on your managed instances, which can be scheduled to run during a designated patching Maintenance Window.&lt;/p&gt;

&lt;p&gt;Begin by selecting a single account to serve as your management account, alongside a designated AWS Region to act as your management Region. With this chosen management setup, you'll be able to schedule Automation tasks from this central location, directing them towards other AWS accounts and Regions as needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup Resource Groups to Logically Group your Managed Instances
&lt;/h3&gt;

&lt;p&gt;Resource groups serve as a helpful tool for organizing your AWS resources, providing a streamlined approach to managing and automating tasks across numerous resources simultaneously. By categorizing resources based on their function, such as distinguishing between web servers and databases, resource groups simplify operations and reduce the risk of applying patches incorrectly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your &lt;strong&gt;Target account&lt;/strong&gt; and navigate to the service &lt;em&gt;Resource Groups &amp;amp; Tag Editor&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Create a new Resource Group using tags associated with your managed instances. It's essential to have previously tagged the instances you wish to manage.&lt;/li&gt;
&lt;li&gt;Specify the resource type as &lt;em&gt;AWS::EC2::Instance&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;For the tags, ensure you've tagged your instances using the designated Tag Key (e.g., PatchGroup) and Tag Value (e.g., 1). This allows for accurate identification and grouping of instances within the resource group.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5ixfegxnf0d40zbjl8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5ixfegxnf0d40zbjl8v.png" alt="Resource Group" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Preview the Resources and Create the Resource Group&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fah3ps2rgm8k8csvi9qb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fah3ps2rgm8k8csvi9qb0.png" alt="Create RG" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup the Required IAM permissions on Management account
&lt;/h3&gt;

&lt;p&gt;From the selected &lt;strong&gt;Management account&lt;/strong&gt;, you need to provision the administrator automation role that will assume the execution roles on the Targeted accounts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to &lt;em&gt;CloudFormation&lt;/em&gt; Console and Create a stack from  &lt;a href="https://github.com/StintriLamah/aws/blob/main/centralized-multi-account-patching/AWS-SystemsManager-AutomationAdministrationRole.yml" rel="noopener noreferrer"&gt;AWS-SystemsManager-AutomationAdministrationRole&lt;/a&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgteath73xar6bmrflhko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgteath73xar6bmrflhko.png" alt="Create Admin CFN Stack" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure the user you are logged in with should have &lt;code&gt;AmazonSSMAutomationRole&lt;/code&gt; attached, &lt;code&gt;iam:PassRole&lt;/code&gt; and &lt;code&gt;resource-groups:ListGroupResources&lt;/code&gt; actions in order to target Resource Groups and pass the role to the Automation Administrator Role created. Here's an example of an inline policy you can create to achieve this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"resource-groups:ListGroupResources"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"iam:PassRole"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:iam::&amp;lt;ManagementAccountId&amp;gt;:role/AWS-SystemsManager-AutomationAdministrationRole"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to replace &lt;code&gt;&amp;lt;ManagementAccountId&amp;gt;&lt;/code&gt; in the policy below with the account ID of the management account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup the Required IAM permissions on Target account
&lt;/h3&gt;

&lt;p&gt;To provision the Execution IAM role in the account you wish to target for Automation tasks, you can use the provided CloudFormation template.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to &lt;em&gt;CloudFormation&lt;/em&gt; Console and Create a stack from the &lt;a href="https://github.com/StintriLamah/aws/blob/main/centralized-multi-account-patching/AWS-SystemsManager-AutomationExecutionRole.yml" rel="noopener noreferrer"&gt;AWS-SystemsManager-AutomationExecutionRole&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7poexr9p0ehtj50vsjal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7poexr9p0ehtj50vsjal.png" alt="Create Exec CFN Stack" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provide a name and the &lt;code&gt;&amp;lt;Management Account ID&amp;gt;&lt;/code&gt; in the Parameters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F46a992khl91udhbrysi4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F46a992khl91udhbrysi4.png" alt="Provide ID for Exec" width="624" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create an Automation Document to Execute Patch Baselines
&lt;/h3&gt;

&lt;p&gt;To create an automation document for executing the &lt;code&gt;AWS-RunPatchBaseline&lt;/code&gt; command, follow these steps in the Management Account:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to &lt;em&gt;Systems Manager&lt;/em&gt; and open Documents section from the left Navigation Menu.&lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;Create document&lt;/strong&gt; button to Start Creating an automation document and Select &lt;strong&gt;Automation&lt;/strong&gt; as the document type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbrqsown31c0tck3p3ogd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbrqsown31c0tck3p3ogd.png" alt="Automation Document Creation" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provide a unique name for the Automation Runbook. &lt;/li&gt;
&lt;li&gt;From &lt;a href="https://github.com/StintriLamah/aws/blob/main/centralized-multi-account-patching/Automation-RunPatchBaseline.json" rel="noopener noreferrer"&gt;Automation-RunPatchBaseline&lt;/a&gt;, copy the JSON content and replace the default content with JSON content copied.
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa72cseaxllq8j582lqjs.png" alt="Automation Document JSON" width="800" height="419"&gt;
&lt;/li&gt;
&lt;li&gt;Create runbook to create the Automation document&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Execute Automation to Patch Multi Account Resources
&lt;/h3&gt;

&lt;p&gt;To execute the Automation Runbook on the &lt;strong&gt;Management Account&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From the Left Navigation pane on &lt;em&gt;Systems Manager&lt;/em&gt;, select &lt;strong&gt;Automation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Execute Automation&lt;/strong&gt; to initiate the execution process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the Automation Runbook you previously created (e.g., Automation-RunPatchBaseline) and click Execute Automation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select Multi account and Region &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falb18yl9dw37s0cjpdvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falb18yl9dw37s0cjpdvx.png" alt="Execute Automation Runbook" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;strong&gt;Target accounts and Regions&lt;/strong&gt; section:

&lt;ul&gt;
&lt;li&gt;Provide the &lt;em&gt;Account IDs&lt;/em&gt; of the targeted accounts and specify the Region where EC2 instances are located&lt;/li&gt;
&lt;li&gt;Specify the &lt;em&gt;Automation Execution Role Name&lt;/em&gt; created in the target accounts&lt;/li&gt;
&lt;li&gt;Optionally, specify the number or percentage of locations (account-Region pairs) on which to execute the task simultaneously.&lt;/li&gt;
&lt;li&gt;Optionally, set an error threshold to stop the task after it fails on a specific number or percentage of locations.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1lm9puft4o22rc2uk7n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1lm9puft4o22rc2uk7n.png" alt="Target Accounts and Region" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;strong&gt;Targets&lt;/strong&gt; Section:

&lt;ul&gt;
&lt;li&gt;Choose &lt;em&gt;InstanceID&lt;/em&gt; as the parameter.&lt;/li&gt;
&lt;li&gt;Select &lt;em&gt;Resource Group&lt;/em&gt; as the targets.&lt;/li&gt;
&lt;li&gt;Provide the name of the Resource Group you created earlier as the Resource group&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98ti6hz2ueak6khffznf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98ti6hz2ueak6khffznf.png" alt="Targets" width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;strong&gt;Input parameters&lt;/strong&gt; section:

&lt;ul&gt;
&lt;li&gt;For the parameter &lt;em&gt;AutomationAssumeRole&lt;/em&gt;, provide the IAM role "AWS-SystemsManager-AutomationAdministrationRole" that you previously created.&lt;/li&gt;
&lt;li&gt;Specify &lt;em&gt;Install&lt;/em&gt; as the operation to Scan and Install.&lt;/li&gt;
&lt;li&gt;Optionally, you can provide an "InstallOverrideList" as a list of patches to be installed. This list will override the patches specified by the default patch baseline.&lt;/li&gt;
&lt;li&gt;Optionally, if needed, you can specify the snapshot ID to use for retrieving a patch baseline snapshot.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwoyox7c6acc6plhryqzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwoyox7c6acc6plhryqzc.png" alt="Input Parameters" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;strong&gt;Rate control&lt;/strong&gt; section:

&lt;ul&gt;
&lt;li&gt;Specify the number or percentage of target instances on which to execute the task simultaneously&lt;/li&gt;
&lt;li&gt;Set an error threshold, which will halt the task after it fails on a specific number or percentage of target instances.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fno81fo76znuwp9e3fmaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fno81fo76znuwp9e3fmaw.png" alt="Rate Control" width="800" height="217"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execute the automation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can follow these execution steps using the AWS CLI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make sure you have the AWS CLI installed and configured with the necessary permissions.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the following command to execute the Automation task:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  aws ssm start-automation-execution 
  &lt;span class="nt"&gt;--document-name&lt;/span&gt; &lt;span class="s2"&gt;"Automation-RunPatchBaseline"&lt;/span&gt; 
  &lt;span class="nt"&gt;--target-parameter-name&lt;/span&gt; InstanceId 
  &lt;span class="nt"&gt;--document-version&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;DEFAULT"&lt;/span&gt;
  &lt;span class="nt"&gt;--parameters&lt;/span&gt; &lt;span class="s1"&gt;'{"AutomationAssumeRole":["arn:aws:iam::&amp;lt;Management Account ID&amp;gt;:role/AWS-SystemsManager-AutomationAdministrationRole"],"Operation":["Install"],"SnapshotId":[""],"InstallOverrideList":[""]}'&lt;/span&gt;
  &lt;span class="nt"&gt;--targets&lt;/span&gt; &lt;span class="s1"&gt;'[{"Key":"ResourceGroup","Values":["Test_Patching"]}]'&lt;/span&gt; 
  &lt;span class="nt"&gt;--target-parameter-name&lt;/span&gt; InstanceId 
  &lt;span class="nt"&gt;--max-errors&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt; 
  &lt;span class="nt"&gt;--max-concurrency&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt; 
  &lt;span class="nt"&gt;--target-locations&lt;/span&gt; &lt;span class="s1"&gt;'[{"Accounts":["&amp;lt;TargetAccountIDs&amp;gt;"],"Regions":["&amp;lt;Region&amp;gt;"],"ExecutionRoleName":"AWS-SystemsManager-AutomationExecutionRole"}]'&lt;/span&gt;
  &lt;span class="nt"&gt;--region&lt;/span&gt; &amp;lt;Region&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Replace &lt;strong&gt;Management Account ID&lt;/strong&gt;, &lt;strong&gt;TargetAccountIDs&lt;/strong&gt;, and &lt;strong&gt;Region&lt;/strong&gt; with your actual values.&lt;/p&gt;

&lt;p&gt;On the &lt;strong&gt;Management account&lt;/strong&gt;, you can monitor the execution progress of the Automation task. This allows you to track the status of the task and any associated errors or issues. By keeping an eye on the execution progress, you can ensure that the patching process is proceeding as expected and take necessary actions if any problems arise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7p85vs5029egd81y57p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7p85vs5029egd81y57p.png" alt="Execution Details" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuw1n0gvs90kuee1sod3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuw1n0gvs90kuee1sod3c.png" alt="Execution ID" width="800" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the &lt;strong&gt;Target Account&lt;/strong&gt;, you can observe the execution of the AWS-RunPatchBaseline command triggered by the Automation Document created in the Management Account. This visibility allows you to monitor the progress of the patching operation directly within the targeted environment. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe522fo3vg12za79n00g7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe522fo3vg12za79n00g7.png" alt="Run Command Execution" width="800" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fld30a2tdpwx4ksp2ef67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fld30a2tdpwx4ksp2ef67.png" alt="Run Command ID" width="800" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7k5zkmz2vpwc4gd961o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7k5zkmz2vpwc4gd961o.png" alt="Run Command 2 commands" width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Schedule Event Bridge Invocation during Patching Window
&lt;/h3&gt;

&lt;p&gt;To enhance automation further, you can set up scheduled Patching Maintenance Windows for your organization. This can be achieved by creating a Scheduled EventBridge Rule that triggers a Lambda function. The Lambda function, in turn, initiates the Multi-Account Centralized Automation Flow, automating the patching process at designated times. It will ensure timely and consistent patching across your AWS environment.&lt;/p&gt;

&lt;p&gt;This is the flow of tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the management account, the EventBridge rule is triggered based on the cron or rate-based expression specified.&lt;/li&gt;
&lt;li&gt;The EventBridge rule then invokes a Lambda function, which, in turn, initiates a multi-account and multi-Region Automation workflow.&lt;/li&gt;
&lt;li&gt;The Systems Manager administrator role assumes the execution role in each target account and Region.&lt;/li&gt;
&lt;li&gt;The execution role initiates a Run Command task for AWS-RunPatchBaseline. This command scans for, or installs missing updates on target managed instances based on membership in the provided AWS Resource Group.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You need to deploy the CloudFormation Stack from &lt;a href="https://github.com/StintriLamah/aws/blob/main/centralized-multi-account-patching/Scheduled-Patch-Automation.yaml" rel="noopener noreferrer"&gt;Scheduled-Patch-Automation&lt;/a&gt; to create the following resources: EventBridge rule, IAM service role for Lambda, Lambda function and Automation document to invoke the Command document &lt;code&gt;AWS-RunPatchBaseline&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;strong&gt;Management account&lt;/strong&gt;, navigate to the &lt;em&gt;CloudFormation&lt;/em&gt; console and create a stack.&lt;/li&gt;
&lt;li&gt;Upload the &lt;code&gt;Scheduled-Patch-Automation.yaml&lt;/code&gt;  template file, and then choose Next.&lt;/li&gt;
&lt;li&gt;Specify the stack details:

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;EventBridgeRuleSchedule&lt;/code&gt;, enter a cron or rate-based expression for the schedule of the EventBridge rule. For example, &lt;code&gt;cron(30 22 ? * SAT *)&lt;/code&gt; schedules the rule to initiate patching on &lt;em&gt;Saturdays at 22:30 UTC&lt;/em&gt;. Choose a cron that matches your patching window.&lt;/li&gt;
&lt;li&gt;Optionally modify the &lt;code&gt;ExecutionRoleName&lt;/code&gt; to match the Automation execution role in target accounts.&lt;/li&gt;
&lt;li&gt;Specify &lt;code&gt;MaximumConcurrency&lt;/code&gt; and &lt;code&gt;MaximumErrors&lt;/code&gt; as needed. You can specify a number, such as 10, or a percentage, such as 10%. The default value is 10%.&lt;/li&gt;
&lt;li&gt;Provide the &lt;code&gt;ResourceGroupName&lt;/code&gt; that includes the resources you want to target.&lt;/li&gt;
&lt;li&gt;Optionally enter an HTTPS or S3 URL for &lt;code&gt;RunPatchBaselineInstallOverrideList&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;RunPatchBaselineOperation&lt;/code&gt;, choose &lt;strong&gt;Scan&lt;/strong&gt; to scan for missing updates only or &lt;strong&gt;Install&lt;/strong&gt; to scan and install missing updates based on the rules of the patch baseline.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;RunPatchBaselineRebootOption&lt;/code&gt;, choose the reboot behavior for the patching operation. The valid options are &lt;strong&gt;RebootIfNeeded&lt;/strong&gt; and &lt;strong&gt;NoReboot&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Enter the list of &lt;code&gt;TargetAccounts&lt;/code&gt; as comma-separated AWS account IDs (for example, &lt;em&gt;012345678901&lt;/em&gt;, &lt;em&gt;987654321098&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;Optionally modify &lt;code&gt;TargetLocationMaxConcurrency&lt;/code&gt; and &lt;code&gt;TargetLocationMaxErrors&lt;/code&gt;. The default value is 1.&lt;/li&gt;
&lt;li&gt;Enter the list of &lt;code&gt;TargetRegionIds&lt;/code&gt; as comma-separated AWS Region names. (for example, &lt;em&gt;us-east-1&lt;/em&gt;, &lt;em&gt;eu-west-1&lt;/em&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioxwzxmexij7pr2semlk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioxwzxmexij7pr2semlk.png" alt="Schedule Automation CFN" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Review the stack details, then click Next. Finally, acknowledge that CloudFormation might create IAM resources and click "Create stack".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the scheduled time is reached, you'll observe the automation being executed in the AWS Systems Manager Automation console.  Here, you'll notice the latest automation document triggered by Lambda and the previous executions initiated manually.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flr69yfy6w7z5fnzreyww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flr69yfy6w7z5fnzreyww.png" alt="Scheduled Automation" width="800" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>security</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
