0
votes

I am automating the AWS IAM user creation from Jenkins. We have multiple Accounts with programmatic access. I am trying to create user account for one account from CLI. It's working fine from CLI, but not from jenkins Pipeline.

Output through CLI

aws iam create-user --user-name --profile test tets

{
    "User": {
    "Path": "/",
    "UserName": "tets",
    "UserId": "AIDAXTRPYNIV6HK7XE43R",
    "Arn": "arn:aws:iam::523012434475:user/tets",
    "CreateDate": "2019-09-18T06:49:41Z"
}



node
{
    stage ('creating IAM user'){
    sh 'aws iam create-user --user-name --profile test $Username'       
}

The config profile (test) could not be found is the error i am getting from Jenkins Pipeline.

AWS CLI is configured on jenkins server. From ssh i am able to execute all the commands.

1
Did you try this? stackoverflow.com/a/56632124/10846194 Also, if your Jenkins is on a Linux box, configure the aws profile for Jenkins user: sudo su – jenkins aws configure Reference - docs.aws.amazon.com/systems-manager/latest/userguide/… - st_rt_dl_8
Is your jenkins is run by the same user as your ssh user? - Juned Ahsan
The same issue can be found in the below link. So just configure the cli profile for your Jenkins user and not ec2-user (Assuming) and you should be good. forums.aws.amazon.com/thread.jspa?messageID=700690 - st_rt_dl_8
No, how do i configure the same for Jenkins user? - rahuls36
Depends upon the host OS on which you are running Jenkins. If it's linux, then just switch to Jenkins user by typing sudo su -jenkins and then typing aws configure and entering the AK and SAK and if it's Windows then the first SO link I posted in my first comment should help. - st_rt_dl_8

1 Answers

1
votes

I've just hit this problem too. Took an hour to unpick but FWIW in the interests of saving others' time (providing they have the same setup):

We use jenkins on an ec2 instance in AWS (built with "ubuntu" as the user). The actual home dir for jenkins is mounted on /data/jenkins

ssh'ing on to the server as ubuntu user puts you in shell as ubuntu. cd ~ # takes you to the home directory (you're already in) pwd # shows you in /home/ubuntu sudo su -jenkins # change to jenkins user cd ~ # actually still takes you to /home/ubuntu pwd # prooves you're still in /home/ubuntu

Creating the .aws folder with config and credentials at this location means you can't use the --profile switch with aws cli commands in the pipeline. The jenkins home dir is actually /data/jenkins so when the pipeline runs, calls to the aws cli will be looking for the .aws folder in /data/jenkins

Copying the .aws folder from /home/ubuntu to /data/jenkins, chowning objects for jenkins:jenkins and ensuring read perms (I used chmod 660) means that when the cli runs, it should see the config in /data/jenkins and the annoying "The config profile could not be found"