I am new to the cloud and trying to create EC2 using Cloud Formation, I am able to create the Instance but I am also trying to configure aws credentials because I am using a LabRole so I don't have permission to create I::AM user to have access to S3 for EC2, SO I am trying to give the credentials during cloud formation.

Resources:

  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: 'ami-0f34c5ae932e6f0e4' # Amazon Linux 2 AMI (Free Tier eligible)
      InstanceType: 't2.micro' # t2.micro instance type (Free Tier eligible)
      KeyName: !Ref MyEC2KeyPair # Reference the created key pair here
      Tags:
        - Key: Name
          Value: term-project-backend # Replace with your desired instance name
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          # Install Node.js 18.x using yum
          curl -sL https://rpm.nodesource.com/setup_18.x | bash -
          yum install -y nodejs
          #!/bin/bash
          echo export aws_access_key_id=VAL1 >> /etc/profile
          echo export aws_secret_access_key=VAL2 >> /etc/profile
          mkdir .aws
          cd .aws
          echo "[default]\naws_access_key_id=VAL1\naws_secret_access_key=VAL2 > .credentials
      
          # Fetch the code from the S3 bucket
          aws s3 cp s3://${CodeBucketName}/${CodeObjectKey} /home/ec2-user/
          unzip /home/ec2-user/backend.zip -d /home/ec2-user/backend
          cd /home/ec2-user/backend

When I connect through the terminal, I do not see anything like .aws/credentials or S3. code. I have tried everything. But I have lack of knowledge related to this and I need help.

I know, I can do it manually but I want it to be done using cloud formation. How can I create .aws/credential file??

Note: I dont have permission to create New Role, I only have LabRole, IamInstanceProfile

0

There are 0 best solutions below